Calling child class from parent class

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Is this possible?

    public $m_rules;    
    public $m_names;     //array of GTCM_GNP object names
    public $m_cnt;
public  function __construct()
        $this->m_rules = array();
        $this->m_names = array();
        $this->m_cnt = 0;
public    function register($name, $rule, $dat_file, $server, $port)
        if(in_array($name, $this->m_names))
            $this->m_rules[$name][] = $rule;
            return $this->m_names[$name];
        $$name = new GTCM_GNP($this);
          return FALSE;
        $this->m_names[] = $name;
        $this->m_rules[$name] = array($rule);
        return $$name;
        private $m_server;         
    private $m_port;           
    private $m_dat_file;    
    private $m_socket;    
    private $m_buffer;        
    private $m_proc_num;    
    private $m_open_region;
    private $m_cur_region_no;
public    function __construct($name = null, $rule = null, $dat_file, $server,  
(initialization code)

public function do_something
      return $this->variable;
Basically I need to do: GNP_DIRECTORY[$$names]->do_something();

or I want to do some method in GTCM_GNP by referring to a specific

Will this (pseudo)code work or is there a better approach.



Re: Calling child class from parent class

Quoted text here. Click to load it

It won't work because, as you said, the method isn't in the parent

Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++

Re: Calling child class from parent class wrote:

Quoted text here. Click to load it

Try to post a *reduced* *working* example next time.

So, first of all, why do you not try and see?

IIUC, in GNP_DIRECTORY::register() you want to append a new GTCM_GNP  
instance to an array property of an GNP_DIRECTORY instance, and let it know  
about the GNP_DIRECTORY instance it is related to:

  public function register($name, $rule, $dat_file, $server, $port)
    $item = new GTCM_GNP($this);
    $this->m_items[$name] = $item;

Then you can of course call methods of the GTCM_GNP instance by using its  
assigned name ($name) as index:

  $dir = new GNP_DIRECTORY();
  $dir->register($name, ?);

I would strongly suggest that the properties be ?private? or
though, and access to them be only possible through a getter, and if not  
read-only, a setter.

I would also suggest renaming the classes if possible; only constants should  
have identifiers that are all-uppercase.  Use ?GNP_Directory?, for example.
And, if possible, avoid cryptic class identifiers like ?GTCM_GNP?; source  
code should ideally be self-explanatory.

If you are looking for something else:
< .

And get a real name, please.

Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.

Re: Calling child class from parent class

On Thursday, April 11, 2013 3:48:24 PM UTC-4, wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

Actually I combined the two classes into one (I don't understand why the or
iginal programmer did not do that in the first place perhaps because it was
 written for php version 4.0.6 and also made heavy use of superglobals in h
is code) And actually the names are not cryptic IF read in the context of w
hat the code does. It actually manipulates a gnp_directory and gtcm_gnp obj
ects. I probably will change the case of the classes however, because "shou
ting" can be annoying!


Re: Calling child class from parent class wrote:

Quoted text here. Click to load it

Probably not a good idea.

Quoted text here. Click to load it

AIUI, it does make sense to have two classes, one whose instance holds  
metadata about other objects and the other one whose instances hold the  
payload.  This is called /aggregation/, or, if it implies ownership,  

The design error apparently made here is to have one class inherit from the  
other (?is-a?, not ?has-a? relationship) and, instead of passing the  
(reference to the) instance to the register() method, have the instance  
created by that method, which I think violates the Law of Demeter (the other  
class's instance does not exist until register() is called, therefore it  
would not be a ?direct component object? ? CMIIW).  That is,

  $dir = new GNP_DIRECTORY();
  $gtcm = new GTCM_GNP($dat_file, $server, $port);
  $dir->register($name, $rule, $gtcm);

would be cleaner.

Quoted text here. Click to load it

Highly unlikely.  There were no visibility specifiers in PHP 4:


Quoted text here. Click to load it

If they are *superglobals* instead of *simple* globals, there is nothing  
wrong with that.  However, there might be reached a point where you would  
want to cache the superglobal value, for example in a local variable, in  
favor of DRY.

Quoted text here. Click to load it

?gtcm_gnp? still looks cryptic to me.  You have to *explain* to me ? that  
is, another developer ? what it *is* (I *have* to read the documentation),  
and that is the problem with it.  (What does it mean, anyway?)

Quoted text here. Click to load it

That is _not_ the reason why you should do it.  You need to distinguish  
between programming and natural languages.

Please learn to quote and get a real name.

var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

Re: Calling child class from parent class

On 4/11/2013 3:48 PM, wrote:
Quoted text here. Click to load it


Think about it.  What happens if the user creates a class of  
GNP_DIRECTORY?  There is no GTCM_GNP, so there is no method to call.  
The same could be true if you have a different class derived from  

You could use polymorphism and create the method in GNP_DIRECTORY; you  
can then override it as necessary in GTCM_GNP (and other derived classes).

But any time you have a base class dependent on a derived class, you  
have a design problem.  Derived classes can (and do) depend on the base  
class but base classes should never depend on something specific in a  
derived class.  That class may or may not exist.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline