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
Return to the default flat view Create a new topic Submit Reply
Re: Calling child class from parent class [message #181097 is a reply to message #181092] Fri, 12 April 2013 02:49 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
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
=================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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 02:07:07 GMT 2024

Total time taken to generate the page: 0.04972 seconds