SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP - acessing creator object

    Yesterday when I was working on a project I ran into a question that I haven't been able to get an answer from the PHP website (maybe I missed it).

    For example, I have this object that creates another.

    PHP Code:
    class {

    var 
    $template;

    function 
    createobj() 
    {

    $b = new b();

    }

    }

    class 
    {

    function 
    accesstemplate()
    {

     
    how do I access the template property from object a??

    }

    }



    How do I access a property from the creator object automaticly? I mean without passing it as a parameter in the accesstemplate function?


    I hope I was able to explain this correctly and I hope that someone can shed a light on this matter because I really depend on this right now.

  2. #2
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't. Not unless $b holds a reference to $a. In your example $b is unaware of the existence of $a. Maybe pass $a as a parameter to the accesstemplate() method of $b.

  3. #3
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or use composition - create a inside B
    PHP Code:
    class {
    public 
    $temp;

    function 
    accesstemplate()

    $this->temp = new a;
    $this->temp->template();
    }

    //do other things with $this->temp


    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  4. #4
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for you replies.

    Wouldn't your suggestions create a huge overhead?

    If we have A that creates B that inside creates A again... I'm thinking that this isn't the best solution.

    And passing it as a parameter is something I'm trying to avoid.

    Isn't there another way?

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally I wouldn't have A create an instance of B or have B needing access to A's properties. If B needs to work with a property of A then either the property should be a property of B or all B's methods that work with the property of A should actually be methods of A. This way you'll avoid 'action at a distance' problems.
    If that makes sense ..

  6. #6
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without a reference to obj $a in the obj $b, no there isn't any other way (that i know of). Maybe you should ask in the advanced php forum.

  7. #7
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    markl999, what you said made some sense but the porpuse of B in this case was to some specific actions that I want to remove from A so I don't have A with loads of methods.

    This is for a type of plugin system.

    Image A has an HTML template in property $template and I just "registered" a plugin B to performe some regex search and replaces on that same $template.

    Does it make sense at all to use this approach?

  8. #8
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP does not natively support accessing the caller and doesn't natively support AOP, so you have to use some kind of mediator between the plugin and the object it wants to plug into. Depending on the scope & scale of the plugin system, the mediator can be either in the object itself or separate from it.

    Here is a really quick example of having the object itself be the mediator:
    PHP Code:
    class {

        protected 
    $template;

        protected 
    $plugins = array();

        public function 
    registerPlugin($obj) {
            if (
    is_object($obj) === false) {
                
    $obj = new $obj();
            }

            
    $this->plugins[] = $obj;
        }


        public function 
    doSomethingWithTemplate() {
            
    // doing something

            // now do something in the plugins
            
    foreach ($this->plugins as $plugin) {
                if (!
    method_exists($plugin'doSomethingWithTemplate')) {
                    continue;
                }

                
    // set the template
                
    $plugin->setTemplate($this->template);

                
    // run the plugin method;
                // if the return is not void, set it as the new template
                
    if (($return $plugin->doSomethingWithTemplate()) !== null) {
                    
    $this->template $return;
                }
            }
        }

    }


    class 
    APlugin {

        protected 
    $template;

        public function 
    setTemplate($template) {
            
    $this->template $template;
        }

    }

    class 
    extends APlugin {

        public function 
    doSomethingWithTemplate() {
            return 
    preg_replace('/<search>/''replaced by'$this->template);
        }

    }

    class 
    extends APlugin {

        public function 
    doSomethingWithTemplate() {
            if (
    $someCondition === true) {
                return 
    str_replace('search''replace'$this->template);
            }
        }

    }

    $a = new A();
    $a->registerPlugin(new B());
    $a->registerPlugin(new C());
    $a->doSomethingWithTemplate(); 
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite


Bookmarks

Posting Permissions

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