Основы
Каждое определение класса начинается с ключевого слова class, затем следует имя класса, и далее пара фигурных скобок, которые заключают в себе определение свойств и методов этого класса.
Именем класса может быть любое слово, при условии, что оно не входит в список зарезервированных слов PHP, начинается с буквы или символа подчеркивания и за которым следует любое количество букв, цифр или символов подчеркивания. Если задать эти правила в виде регулярного выражения, то получится следующее выражение: ^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$.
Класс может содержать собственные константы, переменные (называемые свойствами) и функции (называемые методами).
Пример #1 Простое определение класса
class SimpleClass
// объявление свойства
public $var = ‘значение по умолчанию’ ;
?php
// объявление метода
public function displayVar () echo $this -> var ;
>
>
?>
Псевдо-переменная $this доступна в том случае, если метод был вызван в контексте объекта. $this является ссылкой на вызываемый объект. Обычно это тот объект, которому принадлежит вызванный метод, но может быть и другой объект, если метод был вызван статически из контекста другого объекта. Это показано на следующих примерах:
Пример #2 Переменная $this
class A
function foo ()
if (isset( $this )) echo ‘$this определена (‘ ;
echo get_class ( $this );
echo «)\n» ;
> else echo «\$this не определена.\n» ;
>
>
>
?php
class B
function bar ()
// Замечание: следующая строка вызовет предупреждение, если включен параметр E_STRICT.
A :: foo ();
>
>
// Замечание: следующая строка вызовет предупреждение, если включен параметр E_STRICT.
A :: foo ();
$b = new B ();
$b -> bar ();
// Замечание: следующая строка вызовет предупреждение, если включен параметр E_STRICT.
B :: bar ();
?>
Результат выполнения данного примера:
$this определена (A) $this не определена. $this определена (B) $this не определена.
new
Для создания экземпляра класса используется директива new. Новый объект всегда будет создан, за исключением случаев, когда он содержит конструктор, в котором определен вызов исключения в случае ошибки. Рекомендуется определять классы до создания их экземпляров (в некоторых случаях это обязательно).
Если с директивой new используется строка ( string ), содержащая имя класса, то будет создан новый экземпляр этого класса. Если имя находится в пространстве имен, то оно должно быть задано полностью.
Пример #3 Создание экземпляра класса
// Это же можно сделать с помощью переменной:
$className = ‘Foo’ ;
$instance = new $className (); // Foo()
?>
В контексте класса можно создать новый объект через new self и new parent.
Когда происходит присвоение уже существующего экземпляра класса новой переменной, то эта переменная будет указывать на этот же экземпляр класса. Тоже самое происходит и при передаче экземпляра класса в функцию. Копию уже созданного объекта можно создать через ее клонирование.
Пример #4 Присваивание объекта
$instance = new SimpleClass ();
$assigned = $instance ;
$reference =& $instance ;
$instance -> var = ‘$assigned будет иметь это значение’ ;
$instance = null ; // $instance и $reference становятся null
var_dump ( $instance );
var_dump ( $reference );
var_dump ( $assigned );
?>
Результат выполнения данного примера:
NULL NULL object(SimpleClass)#1 (1) < ["var"]=>string(30) "$assigned будет иметь это значение" >
В PHP 5.3.0 введены несколько новых методов создания экземпляров объекта:
Пример #5 Создание новых объектов
class Test
static public function getNew ()
return new static;
>
>
?php
$obj1 = new Test ();
$obj2 = new $obj1 ;
var_dump ( $obj1 !== $obj2 );
$obj3 = Test :: getNew ();
var_dump ( $obj3 instanceof Test );
$obj4 = Child :: getNew ();
var_dump ( $obj4 instanceof Child );
?>
Результат выполнения данного примера:
bool(true) bool(true) bool(true)
extends
Класс может наследовать методы и свойства другого класса используя ключевое слово extends при его описании. Невозможно наследовать несколько классов, один класс может наследовать только один базовый класс.
Наследуемые методы и свойства могут быть переопределены (за исключением случаев, когда метод класса объявлен как final) путем объявления их с теми же именами, как и в родительском классе. Существует возможность доступа к переопределенным методам или статическим методам путем обращения к ним через parent::
Когда переопределяются методы число и типы аргументов должны оставаться такими же как и были, иначе PHP вызовет ошибку уровня E_STRICT . Это не относится к конструктору, который можно переопределить с другими параметрами.
Пример #6 Простое наследование классов
class ExtendClass extends SimpleClass
// Переопределение метода родителя
function displayVar ()
echo «Расширенный класс\n» ;
parent :: displayVar ();
>
>
?php
$extended = new ExtendClass ();
$extended -> displayVar ();
?>
Результат выполнения данного примера:
Расширенный класс значение по умолчанию
::class
Начиная с версии PHP 5.5 можно использовать ключевое слово class для разрешения имени класса. С помощью конструкции ClassName::class можно получить строку с абсолютным именем класса ClassName. Обычно это довольно полезно при работе с классами, использующими пространства имен.
Пример #7 Разрешение имени класса
Результат выполнения данного примера:
Php class extends this
For the ‘late static binding’ topic I published a code below, that demonstrates a trick for how to setting variable value in the late class, and print that in the parent (or the parent’s parent, etc.) class.
class cA
/**
* Test property for using direct default value
*/
protected static $item = ‘Foo’ ;
/**
* Test property for using indirect default value
*/
protected static $other = ‘cA’ ;
public static function method ()
print self :: $item . «\r\n» ; // It prints ‘Foo’ on everyway. 🙁
print self :: $other . «\r\n» ; // We just think that, this one prints ‘cA’ only, but. 🙂
>
public static function setOther ( $val )
self :: $other = $val ; // Set a value in this scope.
>
>
class cB extends cA
/**
* Test property with redefined default value
*/
protected static $item = ‘Bar’ ;
public static function setOther ( $val )
self :: $other = $val ;
>
>
class cC extends cA
/**
* Test property with redefined default value
*/
protected static $item = ‘Tango’ ;
public static function method ()
print self :: $item . «\r\n» ; // It prints ‘Foo’ on everyway. 🙁
print self :: $other . «\r\n» ; // We just think that, this one prints ‘cA’ only, but. 🙂
>
/**
* Now we drop redeclaring the setOther() method, use cA with ‘self::’ just for fun.
*/
>
class cD extends cA
/**
* Test property with redefined default value
*/
protected static $item = ‘Foxtrot’ ;
/**
* Now we drop redeclaring all methods to complete this issue.
*/
>
cB :: setOther ( ‘cB’ ); // It’s cB::method()!
cB :: method (); // It’s cA::method()!
cC :: setOther ( ‘cC’ ); // It’s cA::method()!
cC :: method (); // It’s cC::method()!
cD :: setOther ( ‘cD’ ); // It’s cA::method()!
cD :: method (); // It’s cA::method()!
Little static trick to go around php strict standards .
Function caller founds an object from which it was called, so that static method can alter it, replacement for $this in static function but without strict warnings 🙂
error_reporting ( E_ALL + E_STRICT );
function caller () $backtrace = debug_backtrace ();
$object = isset( $backtrace [ 0 ][ ‘object’ ]) ? $backtrace [ 0 ][ ‘object’ ] : null ;
$k = 1 ;
return isset( $backtrace [ $k ][ ‘object’ ]) ? $backtrace [ $k ][ ‘object’ ] : null ;
>
function set_data () b :: set ();
>
static function set () // $this->data = ‘Data from B !’;
// using this in static function throws a warning .
caller ()-> data = ‘Data from B !’ ;
>
$a = new a ();
$a -> set_data ();
echo $a -> data ;
You use ‘self’ to access this class, ‘parent’ — to access parent class, and what will you do to access a parent of the parent? Or to access the very root class of deep class hierarchy? The answer is to use classnames. That’ll work just like ‘parent’. Here’s an example to explain what I mean. Following code
class A
protected $x = ‘A’ ;
public function f ()
return ‘[‘ . $this -> x . ‘]’ ;
>
>
class B extends A
protected $x = ‘B’ ;
public function f ()
return ‘ x . ‘>’ ;
>
>
class C extends B
protected $x = ‘C’ ;
public function f ()
return ‘(‘ . $this -> x . ‘)’ . parent :: f (). B :: f (). A :: f ();
>
>
$a = new A ();
$b = new B ();
$c = new C ();
Nice trick with scope resolution
class A
public function TestFunc ()
return $this -> test ;
>
>
public function __construct ()
$this -> test = «Nice trick» ;
>
public function GetTest ()
return A :: TestFunc ();
>
>
$b = new B ;
echo $b -> GetTest ();
?>
will output
This is a solution for those that still need to write code compatible with php 4 but would like to use the flexibility of static variables. PHP 4 does not support static variables within the class scope but it does support them within the scope of class methods. The following is a bit of a workaround to store data in static mode in php 4.
Note: This code also works in PHP 5.
The tricky part is when using when arrays you have to do a bit of fancy coding to get or set individual elements in the array. The example code below should show you the basics of it though.
class StaticSample
//Copyright Michael White (www.crestidg.com) 2007
//You may use and modify this code but please keep this short copyright notice in tact.
//If you modify the code you may comment the changes you make and append your own copyright
//notice to mine. This code is not to be redistributed individually for sale but please use it as part
//of your projects and applications — free or non-free.
//Static workaround for php4 — even works with arrays — the trick is accessing the arrays.
//I used the format s_varname for my methods that employ this workaround. That keeps it
//similar to working with actual variables as much as possible.
//The s_ prefix immediately identifies it as a static variable workaround method while
//I’m looking thorugh my code.
function & s_foo ( $value = null , $remove = null )
static $s_var ; //Declare the static variable. The name here doesn’t matter — only the name of the method matters.
if( $remove )
if( is_array ( $value ))
if( is_array ( $s_var ))
foreach( $value as $key => $data )
unset( $s_var [ $key ]);
>
>
>
else
//You can’t just use unset() here because the static state of the variable will bring back the value next time you call the method.
$s_var = null ;
unset( $s_var );
>
//Make sure that you don’t set the value over again.
$value = null ;
>
if( $value )
if( is_array ( $value ))
if( is_array ( $s_var ))
//$s_var = array_merge($s_var, $value); //Doesn’t overwrite values. This adds them — a property of the array_merge() function.
foreach( $value as $key => $data )
$s_var [ $key ] = $data ; //Overwrites values.
>
>
else
$s_var = $value ;
>
>
else
$s_var = $value ;
>
>
echo «Working with non-array values.
» ;
echo «Before Setting: » . StaticSample :: s_foo ();
echo «
» ;
echo «While Setting: » . StaticSample :: s_foo ( «VALUE HERE» );
echo «
» ;
echo «After Setting: » . StaticSample :: s_foo ();
echo «
» ;
echo «While Removing: » . StaticSample :: s_foo ( null , 1 );
echo «
» ;
echo «After Removing: » . StaticSample :: s_foo ();
echo «
» ;
echo «Working with array values
» ;
$array = array( 0 => «cat» , 1 => «dog» , 2 => «monkey» );
echo «Set an array value: » ;
print_r ( StaticSample :: s_foo ( $array ));
echo «
» ;
//Here you need to get all the values in the array then sort through or choose the one(s) you want.
$all_elements = StaticSample :: s_foo ();
$middle_element = $all_elements [ 1 ];
echo «The middle element: » . $middle_element ;
echo «
» ;
$changed_array = array( 1 => «big dog» , 3 => «bat» , «bird» => «flamingo» );
echo «Changing the value: » ;
print_r ( StaticSample :: s_foo ( $changed_array ));
echo «
» ;
//All you have to do here is create an array with the keys you want to erase in it.
//If you want to erase all keys then don’t pass any array to the method.
$element_to_erase = array( 3 => null );
echo «Erasing the fourth element: » ;
$elements_left = StaticSample :: s_foo ( $element_to_erase , 1 );
print_r ( $elements_left );
echo «
» ;
echo «Enjoy!» ;
- Классы и объекты
- Введение
- Основы
- Свойства
- Константы классов
- Автоматическая загрузка классов
- Конструкторы и деструкторы
- Область видимости
- Наследование
- Оператор разрешения области видимости (::)
- Ключевое слово static
- Абстрактные классы
- Интерфейсы объектов
- Трейты
- Анонимные классы
- Перегрузка
- Итераторы объектов
- Магические методы
- Ключевое слово final
- Клонирование объектов
- Сравнение объектов
- Позднее статическое связывание
- Объекты и ссылки
- Сериализация объектов
- Ковариантность и контравариантность
- Журнал изменений ООП