www.webdeveloper.com
Results 1 to 2 of 2

Thread: Issues with dynamic properties

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    4

    Issues with dynamic properties

    Hello Guys;

    I've being breaking my head for several days already trying to make this work a but it doesn't.

    The question is as follow:

    I need to create a property within a class, this property should be dynamically created and it have to be use in any other class extending this class, I'm posting the following sample to give you an idea of how it should look and work.

    class A
    {
    public function __construct()
    {
    $this->config = $this->config();
    }

    public function config()
    {
    $config = array(
    'name' => 'this name',
    'url' => 'http://sample.com'
    );
    return $config;
    }
    }
    class B extends A
    {
    public function __construct(){}

    public function get_config()
    {
    return $this->config;
    }
    }
    $B = new B;
    echo $B->get_config();

    Any help is greatly appreciated!!!

    Thanks in advance.

  2. #2
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    You would need to define it as a property of the class at definition, not just in the scope of that instance of an object.

    Second, it could not be dynamically generated, and have those dynamic values be accessible through child classes. In order to inherit from the parent, it has to be available at the time of definition of the child class.

    What you're asking for is a way to dynamically set something, but yet you want to be able to access it statically (aka in other instances) but this is NOT what classes do. Each instance is its own beast. However, what you could do it call parent::method(); to use the parents method of generating the property's value(s).

    Finally, please use the boards [php][/php], [html][/html], or [code][/code] tags where appropriate as they mark the up much easier to read and analyze.
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles