Jump to content


Photo

best way to inherit an instantiated object?


  • Please log in to reply
2 replies to this topic

#1 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 05 September 2006 - 04:59 AM

Hello,

I need to be able to extend an object, but all the inherited data in the child object must allread be instantiated. 

What are the best methods/practices to do this? The reason why I ask, is I am unable to use a constructor in the child. The child object needs to be formed in such a way that it can be made "callable" by client side Flash code. Constructors will break this functionality, but it will call an init() method if it exists.

Can any one recommend the best way to get the data in the child object instantiated?  If I just call the parent constructor from a method in a child object, will the inherited data in the child object be instantiated as well? Should I just get rid of the parent constructor and put it in the init() function of the child? I would prefer not to do the later since the constructor of the parent uses some private helper functions that I would have to make public.

Thanks for your time!
...drkstr

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 05 September 2006 - 08:24 AM

What's stopping you using the constructor?!

<?php

class Foo
{
    protected $bar;

    public function __construct ()
    {
        $this->bar = 'foobar';
    }
}
?>


#3 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 05 September 2006 - 04:31 PM

Like I said, using a constructor in the child object breaks the Flash remoting (amfphp). I'm not sure why, but it does.

right now I am just adding a data member to the child object called myObjectVars, then in the init() method, I'm doing crating an instance of the parent:

class ServiceNetworkConfig extends NetworkConfig {

  private $myObjectVars = array();

  public function init() {
    $netConfig = new NetworkConfig();
    $this->myObjectVars = get_object_vars($netConfig);
  }

  #This function will be callable by the client side Flash script.
  public function retrieveData() {
    return $this->myObjectVars;
  }
}

I don't like this though because it makes parsing the data back to the parent class difficult.

Thanks for the reply!
...drkstr





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users