I have attempted to access an object attribute from another object, getting a notice about the variable (object) being undefined. A scope issue maybe.
The situation is that in one file i instantiate object 1 which is included from a second file (may be irrelevant), alas
$object1 = new object1();
and in a method of object one, which is subsequently called, I instantiate another object of type two (from yet another included file).
$object1 -> method();
Where method contains
$object2 = new object2();
I then run methods of $object2 where I try to access attributes of $object1, and this is what prompts the error.
I can certainly solve the issue itself differently, though I am curious as to what causes this and if it is possible to access attributes of an arbitrary object y from another arbitrary object x. I was under the impression that since everything originates in one file, them being included is in practice equivalent to them all being listed in that file, and further that then all objects defined and instantiated there could be accessed during the same execution.
I think what your trying to do is similar to what the concept of inheritance is, where a sublclass extending its parent class inherits all the public and protected properties and methods of the parent class.
but what you have described is not the way for a class to extend another class.
I know about inheritance, it did not feel intuitive or reasonable to use that in this case. I will consider it again however.
However that may be, I have other objects planned which may or may not be suitable to be a subclass of another class. Is there then anyway to access these attributes, for they are needed application wide.
Maybe I should make them global, that is something I did not consider, I will look into that alternative.
P.S. Basically I have a main application class; ‘Application’ (albeit this is not the actual name). And I then have a page class (again not the actual name but, basically). It feels like they are two different categories altogether, the pages are parts, compartments of the application, bu they are not a variant or specific extension of it. Which is what I feel inheritance ought to be used for. But maybe it is an unfounded or flawed conception.
if you have two instances of two different classes as in
$obj1 = new Object1();
$obj2 = new Object2();
and say a method in $obj2 needs to access a property in $obj1 then you could code the method such that you pass the required value from $obj1 to it or even pass a reference to the $obj1 to the method.
I think if methods in objects start instantiating other objects which then need to reference properties in the object that created the second object, at best things will get messy very quickly assuming you don’t run into scope issues.
To be honest, I’ve never done what you described originally. I’ve either extended a class or done what I describe in the above option.