FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Calling child class from parent class
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Calling child class from parent class [message #181092] Thu, 11 April 2013 19:48 Go to next message
daveh is currently offline  daveh
Messages: 18
Registered: March 2013
Karma: 0
Junior Member
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
Re: Calling child class from parent class [message #181093 is a reply to message #181092] Thu, 11 April 2013 20:40 Go to previous messageGo to next message
Salvatore is currently offline  Salvatore
Messages: 38
Registered: September 2012
Karma: 0
Member
On 2013-04-11, daveh(at)allheller(dot)net <daveh(at)allheller(dot)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++
Re: Calling child class from parent class [message #181094 is a reply to message #181092] Thu, 11 April 2013 21:15 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
daveh(at)allheller(dot)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(at)news(dot)demon(dot)co(dot)uk>
Re: Calling child class from parent class [message #181096 is a reply to message #181092] Fri, 12 April 2013 00:11 Go to previous messageGo to next message
daveh is currently offline  daveh
Messages: 18
Registered: March 2013
Karma: 0
Junior Member
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
Re: Calling child class from parent class [message #181097 is a reply to message #181092] Fri, 12 April 2013 02:49 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 4/11/2013 3:48 PM, daveh(at)allheller(dot)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(at)attglobal(dot)net
=================
Re: Calling child class from parent class [message #181098 is a reply to message #181096] Fri, 12 April 2013 09:58 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
daveh(at)allheller(dot)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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Calling a php file from another on apache server
Next Topic: Re: eine PHP mit video faehigkeit
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 01:54:26 GMT 2024

Total time taken to generate the page: 0.31374 seconds