Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php object within object, getting the top level object instance

Tags:

object

oop

php

I am experimenting with PHP OOP

what i'm trying to find out is, Is it possible to access a object instance from withing a object that was created in this object instance?

sounds confusing, so here is a example:

contents of index


    class mainclass {
        var $data;
        function __construct($data){
            $this->data = $data;
        }
        function echodata(){
            echo $this->data;
        }
        function start($classname){
            include $classname.'.php';
            $inner = new $classname();
            $inner->show();
        }

    }

    $mainclass = new mainclass('maininfostuff');
    $mainclass->start('innerclass');



    //i want it to echo "maininfostuff"

contents of innerclass.php


class innerclass{
        function show(){
            mainclass->echodata();//the problem is here
        }
}

the purpose of this test case is to make the inner class decide if/when to run the mainclass echodata function

how can the above example be accomplished? (without relying on static classes or singletons or extended classes)

edit: due to some confusion in answers i have edited the example

like image 901
Timo Huovinen Avatar asked Jan 29 '10 14:01

Timo Huovinen


People also ask

How do you access the properties of an object in PHP?

Once you have an object, you can use the -> notation to access methods and properties of the object: $object -> propertyname $object -> methodname ([ arg, ... ] ) Methods are functions, so they can take arguments and return a value: $clan = $rasmus->family('extended');

How will you access the reference to same object within the object in PHP?

By comparing the two objects with the == operator, you can see if they have the same classes and attributes. if($box1 == $box2) echo 'equivalent'; You can further distinguish whether they refer to the same original object, and compare them in the same way: === operator: if($box1 === $box2) echo 'exact same object!

How do you find the value of the StdClass object?

You create StdClass objects and access methods from them like so: $obj = new StdClass; $obj->foo = "bar"; echo $obj->foo; I recommend subclassing StdClass or creating your own generic class so you can provide your own methods. Thank you for your help!

What is a StdClass object?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.


1 Answers

Inner classes are not possible in PHP. So you cannot declare your innerclass within mainclass. And since you can't do that, there's no way to access the outer classes variables as you can in, for example, Java.

You can do this instead:

 class mainclass {
     var $data;
     function __construct($data){
         $this->data = $data;
     }
     function echodata(){
         echo $this->data;
     }
     function start(){
         $inner = new innerclass();
         $inner->show($this);
     }

 }
 class innerclass{
     function show($mainclass){
         $mainclass->echodata();//the problem is here
     }
 }

 $mainclass = new mainclass('maininfostuff');
 $mainclass->start();
like image 162
Scott Saunders Avatar answered Nov 03 '22 02:11

Scott Saunders