Working with PHP class properties

I created a new class called Player with two properties:

  • name

  • ID

I have no idea how the following code worked with me: why am I asking you:

 <?php $player = new Player(); $player->name = "Someone"; $player->ID = 123; $player->color = "Red"; // Why it worked? echo $player->color; // Worked great too! ?> 

I could set the color property and use it when I did not even declare it in my class file. How is this possible?

+5
source share
1 answer

In PHP, this function is called property overloading (the term "overloading" was used incorrectly , it should have been something like "interpreter interceptors").

PHP overloading provides the ability to dynamically "create" properties and methods. qoute

You can use dynamic properties as if they were declared in a class definition; as:

 class A { } $a = new A(); $a->myProperty = 'something'; 
+3
source

Source: https://habr.com/ru/post/1234313/


All Articles