Re: Calling child class from parent class [message #181097 is a reply to message #181092] |
Fri, 12 April 2013 02:49 |
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
=================
|
|
|