Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9035 > unrolled thread

Calling child class from parent class

Started bydaveh@allheller.net
First post2013-04-11 12:48 -0700
Last post2013-04-11 22:49 -0400
Articles 6 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Calling child class from parent class daveh@allheller.net - 2013-04-11 12:48 -0700
    Re: Calling child class from parent class Salvatore <sal@yojimbo.hack.invalid> - 2013-04-11 20:40 +0000
    Re: Calling child class from parent class Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-11 23:15 +0200
    Re: Calling child class from parent class daveh@allheller.net - 2013-04-11 17:11 -0700
      Re: Calling child class from parent class Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-12 11:58 +0200
    Re: Calling child class from parent class Jerry Stuckle <jstucklex@attglobal.net> - 2013-04-11 22:49 -0400

#9035 — Calling child class from parent class

Fromdaveh@allheller.net
Date2013-04-11 12:48 -0700
SubjectCalling child class from parent class
Message-ID<1d7f9899-6d51-4d2d-b9a4-6e63ba979f26@googlegroups.com>
Is this possible?

class GNP_DIRECTORY
{
    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);
		if(!$$name)
		  {
		  return FALSE;
		  }
		$this->m_cnt++;
		$this->m_names[] = $name;
		$this->m_rules[$name] = array($rule);
		return $$name;
	}
}
class GTCM_GNP extends GNP_DIRECTORY
{
        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, 
$port)
  {
(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
GNP_DIRECTORY

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

Thanks

  

[toc] | [next] | [standalone]


#9036

FromSalvatore <sal@yojimbo.hack.invalid>
Date2013-04-11 20:40 +0000
Message-ID<kk774k$17lv$1@yojimbo.hack>
In reply to#9035
On 2013-04-11, daveh@allheller.net <daveh@allheller.net> wrote:
> Will this (pseudo)code work or is there a better approach.

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

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

[toc] | [prev] | [next] | [standalone]


#9037

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-11 23:15 +0200
Message-ID<5271727.vFKcIDWhIK@PointedEars.de>
In reply to#9035
daveh@allheller.net wrote:

> Is this possible?
> 
> […]
> class GNP_DIRECTORY
> {
>     public $m_rules;    
>     public $m_names;    //array of GTCM_GNP object names
>     public $m_cnt;
>  […]
>     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);
>                 if(!$$name)
>                   {
>                   return FALSE;
>                   }
>                 $this->m_cnt++;
>                 $this->m_names[] = $name;
>                 $this->m_rules[$name] = array($rule);
>                 return $$name;
>         }
> }
> 
> class GTCM_GNP extends GNP_DIRECTORY
> {
>   […]
>     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
> GNP_DIRECTORY
> 
> Will this (pseudo)code work or is there a better approach.

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, …);
  $dir->m_items[$name]->do_something();

I would strongly suggest that the properties be “private” or “protected”, 
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:
<http://www.catb.org/~esr/faqs/smart-questions.html#beprecise>.

And get a real name, please.


PointedEars
-- 
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.
  -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

[toc] | [prev] | [next] | [standalone]


#9039

Fromdaveh@allheller.net
Date2013-04-11 17:11 -0700
Message-ID<6f4737e3-b39b-4dd1-a765-f146c544f762@googlegroups.com>
In reply to#9035
On Thursday, April 11, 2013 3:48:24 PM UTC-4, da...@allheller.net wrote:
> Is this possible?
> 
> 
> 
> class GNP_DIRECTORY
> 
> {
> 
>     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);
> 
> 		if(!$$name)
> 
> 		  {
> 
> 		  return FALSE;
> 
> 		  }
> 
> 		$this->m_cnt++;
> 
> 		$this->m_names[] = $name;
> 
> 		$this->m_rules[$name] = array($rule);
> 
> 		return $$name;
> 
> 	}
> 
> }
> 
> class GTCM_GNP extends GNP_DIRECTORY
> 
> {
> 
>         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, 
> 
> $port)
> 
>   {
> 
> (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
> 
> GNP_DIRECTORY
> 
> 
> 
> Will this (pseudo)code work or is there a better approach.
> 
> 
> 
> Thanks

Actually I combined the two classes into one (I don't understand why the original 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 his code) And actually the names are not cryptic IF read in the context of what the code does. It actually manipulates a gnp_directory and gtcm_gnp objects. I probably will change the case of the classes however, because "shouting" can be annoying!

Dave

[toc] | [prev] | [next] | [standalone]


#9041

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-12 11:58 +0200
Message-ID<3710226.RFm44XWl4p@PointedEars.de>
In reply to#9039
daveh@allheller.net wrote:

> Actually I combined the two classes into one

Probably not a good idea.

> (I don't understand why the original programmer did not do that in the
> first place

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, 
/composition/.

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.

> perhaps because it was written for php version 4.0.6

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

<http://www.php.net/manual/en/oop5.intro.php>

> and also made heavy use of superglobals in his code)

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.

> And actually the names are not cryptic IF read in the context
> of what the code does. It actually manipulates a gnp_directory and
> gtcm_gnp objects.

“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?)

> I probably will change the case of the classes however,
> because "shouting" can be annoying!

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.


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

[toc] | [prev] | [next] | [standalone]


#9040

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-04-11 22:49 -0400
Message-ID<kk7sj2$l98$1@dont-email.me>
In reply to#9035
On 4/11/2013 3:48 PM, daveh@allheller.net wrote:
> Is this possible?
>
> class GNP_DIRECTORY
> {
>      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);
> 		if(!$$name)
> 		  {
> 		  return FALSE;
> 		  }
> 		$this->m_cnt++;
> 		$this->m_names[] = $name;
> 		$this->m_rules[$name] = array($rule);
> 		return $$name;
> 	}
> }
> class GTCM_GNP extends GNP_DIRECTORY
> {
>          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,
> $port)
>    {
> (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
> GNP_DIRECTORY
>
> Will this (pseudo)code work or is there a better approach.
>
> Thanks
>
>
>

Dave,

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 
GNP_DIRECTORY.

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.
jstucklex@attglobal.net
=================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web