Accessing object attributes from another object

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.

The attributes are public.

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.

Thank you for the answer.

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.

another option.

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.

Global doesn’t seem to work within objects (?), I am leaning towards the second option you posted, so that all objects for which they are significanct are constructed with the attributes in question.

yep I think using inheritance or option 2 is the way to go.

the original scenario you described is not something I would be keen to do, even if it worked, because you could get into a tangled code mess very easily imho.

A third option to globals would be to use a separate class with such attributes declared as static. I have not tested this though.

yep, as long as you realise that multiple instances of the same class that has static properties will share the one copy of those static properties.

in this case each instance would not have its own copy of the static properties.

I decided to try this use of namespaces and have a new question.

The attributes are sufficed with a number and need to be iterated and defined in loops. Something like the following

class3::$current_attribute = whatever that is to be set to this attribute.

However, PHP treats $current_attribute literally (which is reasonable), how can I make that part vary with the loop count. I basically need to assign to

class3::$attribute#

where # is the current loop number. I could use arrays in this case. But is there anyway to make the namespace syntax accept such variations?

edit:

Nevermind the overlook, clearly one needs to type class3::$$current_attribute.

Thanks for the input.