There are for example the main class and classes that extend it:

class mainClass { public static function some_function() { $obj = new mainClass(); $obj->property1 = 'Value of property1'; $obj->property2 = 'Value of property2'; return $obj; } } $mainObj = new mainClass(); class addedClass1 extends mainClass { public static function some_function() { $obj = mainClass::some_function(); $obj->property1 = 'Overrided value of property1'; return $obj; } } class addedClass2 extends mainClass { public static function some_function() { $obj = mainClass::some_function(); $obj->property2 = 'Overrided value of property2'; return $obj; } } $arrayOfClasses = ['addedClass1','addedClass2']; foreach($arrayOfClasses as $class) { $mainObj = new $class(); } echo $mainObj->some_function()->property1 .'<br>'; echo $mainObj->some_function()->property2; 

In this case, the class addedClass2 rewrites $obj->property2 as it should, but addedClass1 no longer overwrites $obj->property1

Those. now I get:

 Value of property1 Overrided value of property2 

And you need:

 Overrided value of property1 Overrided value of property2 

How to make all classes connect and overwrite each of its own separate property?

ps. addedClass will actually connect everything from a separate directory, so neither their names nor the total number are known, these two are for example only

    1 answer 1

    The problem is that you rewrite the $ mainObj variable in the loop, and at the exit from the loop, the value after the last iteration is stored in it. To get both objects, you can either assemble them into an array in the same cycle (option 1) , or declare static properties in the class, and write the necessary values ​​to these properties (option 2) :

    Option number 1

     class mainClass { public static function some_function() { $obj = new mainClass(); $obj->property1 = 'Value of property1'; $obj->property2 = 'Value of property2'; return $obj; } } class addedClass1 extends mainClass { public static function some_function() { $obj = mainClass::some_function(); $obj->property1 = 'Overrided value of property1'; return $obj; } } class addedClass2 extends mainClass { public static function some_function() { $obj = mainClass::some_function(); $obj->property2 = 'Overrided value of property2'; return $obj; } } $arrayOfClasses = ['addedClass1','addedClass2']; foreach ($arrayOfClasses as $class) { $mainObj[] = new $class(); } echo $mainObj[0]->some_function()->property1 .'<br>'; echo $mainObj[1]->some_function()->property2; /* Overrided value of property1 Overrided value of property2 */ 

    Option number 2

     class mainClass { public static $property1; public static $property2; public static function some_function() { self::$property1 = 'Value of property1'; self::$property2 = 'Value of property2'; } } class addedClass1 extends mainClass { public static function some_function() { mainClass::$property1 = 'Overrided value of property1'; } } class addedClass2 extends mainClass { public static function some_function() { mainClass::$property2 = 'Overrided value of property2'; } } $arrayOfClasses = ['addedClass1', 'addedClass2']; foreach ($arrayOfClasses as $class) { $class::some_function(); } echo addedClass1::$property1 .'<br>'; echo addedClass1::$property2; /* Overrided value of property1 Overrided value of property2 */ 
    • Thank! In the end, the truth is better echo mainClass::$property... And another question: is it possible to add $property3 , for example, inside something addedClassX, assign a value to it and stick it to mainClass to output at the end through echo mainClass::$property3 ? - stckvrw
    • @stckvrw In the end, the truth is better than echo mainClass :: $ property - there is no difference, because propertyN is available both there and there. At the expense of the question: is it possible to add something like $ property3, add something to it and stick it to mainClass to output at the end through echo mainClass :: $ property3 - if you need to add values ​​to properties dynamically, then you can act by analogy with arrays, declare one property “array” and operate with keys: see an example - Edward
    • @stckvrw another example: adding inside a class - Edward
    • @stckvrw added an example of property overloading to watch a demo - Edward