This self parent php

Типы данных в PHP: self и parent

Начиная с PHP 5.0 , мы можем указывать типы данных аргументов функции, а с выходом новых версий PHP количество возможных type hints увеличилось. Краткий ликбез в PHP self :

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
    • Клонирование объектов
    • Сравнение объектов
    • Позднее статическое связывание
    • Объекты и ссылки
    • Сериализация объектов
    • Ковариантность и контравариантность
    • Журнал изменений ООП

    Источник

Оцените статью