Skip to content

Instantly share code, notes, and snippets.

@CrazyBoy49z
Created July 7, 2018 04:14
Show Gist options
  • Save CrazyBoy49z/0118cd8f3abdb323d51800437e2a0d07 to your computer and use it in GitHub Desktop.
Save CrazyBoy49z/0118cd8f3abdb323d51800437e2a0d07 to your computer and use it in GitHub Desktop.
<?php
//Работа с методами-перехватчиками
__get($property) //- Вызывается при обращении к неопределенному свойству
//Пример:
class Gets{
function __get($property){
$method = "get{$property}";
if (method_exist($this, $method)){
return $this->$method();
}
}
function getName(){
return 'name';
}
}
$g = new Gets();
print $g->name;
__set($property, $value) //- Вызывается, когда неопределенному свойству присваиваеться значение
//Пример:
class Gets {
private $name = '';
function __set($property, $value) {
$method = "set{$property}";
if (method_exist($this, $method)) {
return $this->$method($value);
}
}
function setName($value){
$this->name = $value;
}
}
$g = new Gets();
print $g->name = 'name';
__isset($property) //- Вызывается, когда функция isset() вызываеться для неопределенного свойства
//Пример:
class Gets {
function __isset($property){
$method = "get{$property}";
return method_exist($this, $method);
}
}
$g = new Gets();
if (!isset($g->name))
print $g->name;
__unset($property) //- Вызывается, когда функция unset() вызываеться для неопределенного свойства
//Пример:
class Gets {
function __unset($property){
$method = "set{$property}";
if (method_exist($this, $method)) {
$this->$method(null);
}
}
}
$g = new Gets();
unset($g->name);
__call($method, $arg_array) //- Вызывается при обращении к неопределенному методу
//Пример:
//...
get_class() //- Получение информации об классе
get_class_methods() //- Получение информации об методах
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment