I ask those who know php to help, I study php, for me, in general, the first programming language, so the process constantly raises so stupid questions that the Internet doesn't even seem to answer them. He reached the classes and fell into a stupor. Help me to understand.
There is such an example from the textbook:
<?php ## Использование статических членов класса. class Counter { // Скрытый статический член класса - общий для всех объектов. private static $count = 0; // Конструктор увеличивает счетчик на 1. Обратите внимание // на синтаксис доступа к статическим переменным класса! public function __construct() { self::$count++; } // Деструктор же - уменьшает. public function __destruct() { self::$count--; } // Статическая функция, возвращает счетчик объектов. public static function getCount() { return self::$count; } // Как видите, установить счетчик в произвольное значение // извне нельзя, можно только получить его значение. Вот он, // модификатор private "в действии". } // Создаем 6 объектов. for ($objs = [], $i = 0; $i < 6; $i++) $objs[] = new Counter(); // Статические функции можно вызывать точно так же, как будто // бы это - обычный метод объекта. При этом $this все равно // не передается, он просто игнорируется. echo "Сейчас существует {$objs[0]->getCount()} объектов.<br />"; // Удаляем один объект. $objs[5] = null; // Счетчик объектов уменьшится! echo "А теперь - {$objs[0]->getCount()} объектов.<br />"; // Удаляем все объекты. $objs = []; // Другой способ вызова статического метода - с указанием класса. // Это очень похоже на вызов функции из библиотеки. echo "Под конец осталось - ".Counter::getCount()." объектов.<br />"; ?> I'll start from the end:
- $ objs [0] -> getCount () - why is 0 specified?
- $ objs [5] = null; - I understand that the null value is assigned to the fifth element in the $ objs array, but in fact, the entire array seems to be cleaned up, how does it work?
- $ objs [] = new Counter (); - I understand that an array of $ objs is created which uses the class Counter. But what does it mean? As an array can be equal to a class, there is no exact value there. Only a couple of functions.
- public function __destruct () {self :: $ count--; } - as I understand it, reduces $ count by 1, but how does it start ?? What drives this feature?
- public function __construct () {self :: $ count ++; } - Same thing, only increments the counter by 1, the same question that triggers this function?