Php class this scope

Php class this scope

INSIDE CODE and OUTSIDE CODE

class Item
/**
* This is INSIDE CODE because it is written INSIDE the class.
*/
public $label ;
public $price ;
>

/**
* This is OUTSIDE CODE because it is written OUTSIDE the class.
*/
$item = new Item ();
$item -> label = ‘Ink-Jet Tatoo Gun’ ;
$item -> price = 49.99 ;

?>

Ok, that’s simple enough. I got it inside and out. The big problem with this is that the Item class is COMPLETELY IGNORANT in the following ways:
* It REQUIRES OUTSIDE CODE to do all the work AND to know what and how to do it — huge mistake.
* OUTSIDE CODE can cast Item properties to any other PHP types (booleans, integers, floats, strings, arrays, and objects etc.) — another huge mistake.

Note: we did it correctly above, but what if someone made an array for $price? FYI: PHP has no clue what we mean by an Item, especially by the terms of our class definition above. To PHP, our Item is something with two properties (mutable in every way) and that’s it. As far as PHP is concerned, we can pack the entire set of Britannica Encyclopedias into the price slot. When that happens, we no longer have what we expect an Item to be.

INSIDE CODE should keep the integrity of the object. For example, our class definition should keep $label a string and $price a float — which means only strings can come IN and OUT of the class for label, and only floats can come IN and OUT of the class for price.

Читайте также:  Docker supervisor php fpm

class Item
/**
* Here’s the new INSIDE CODE and the Rules to follow:
*
* 1. STOP ACCESS to properties via $item->label and $item->price,
* by using the protected keyword.
* 2. FORCE the use of public functions.
* 3. ONLY strings are allowed IN & OUT of this class for $label
* via the getLabel and setLabel functions.
* 4. ONLY floats are allowed IN & OUT of this class for $price
* via the getPrice and setPrice functions.
*/

protected $label = ‘Unknown Item’ ; // Rule 1 — protected.
protected $price = 0.0 ; // Rule 1 — protected.

public function getLabel () < // Rule 2 - public function.
return $this -> label ; // Rule 3 — string OUT for $label.
>

public function getPrice () < // Rule 2 - public function.
return $this -> price ; // Rule 4 — float OUT for $price.
>

public function setLabel ( $label ) // Rule 2 — public function.
/**
* Make sure $label is a PHP string that can be used in a SORTING
* alogorithm, NOT a boolean, number, array, or object that can’t
* properly sort — AND to make sure that the getLabel() function
* ALWAYS returns a genuine PHP string.
*
* Using a RegExp would improve this function, however, the main
* point is the one made above.
*/

if( is_string ( $label ))
$this -> label = (string) $label ; // Rule 3 — string IN for $label.
>
>

public function setPrice ( $price ) // Rule 2 — public function.
/**
* Make sure $price is a PHP float so that it can be used in a
* NUMERICAL CALCULATION. Do not accept boolean, string, array or
* some other object that can’t be included in a simple calculation.
* This will ensure that the getPrice() function ALWAYS returns an
* authentic, genuine, full-flavored PHP number and nothing but.
*
* Checking for positive values may improve this function,
* however, the main point is the one made above.
*/

if( is_numeric ( $price ))
$this -> price = (float) $price ; // Rule 4 — float IN for $price.
>
>
>

?>

Now there is nothing OUTSIDE CODE can do to obscure the INSIDES of an Item. In other words, every instance of Item will always look and behave like any other Item complete with a label and a price, AND you can group them together and they will interact without disruption. Even though there is room for improvement, the basics are there, and PHP will not hassle you. which means you can keep your hair!

If you have problems with overriding private methods in extended classes, read this:)

The manual says that «Private limits visibility only to the class that defines the item». That means extended children classes do not see the private methods of parent class and vice versa also.

As a result, parents and children can have different implementations of the «same» private methods, depending on where you call them (e.g. parent or child class instance). Why? Because private methods are visible only for the class that defines them and the child class does not see the parent’s private methods. If the child doesn’t see the parent’s private methods, the child can’t override them. Scopes are different. In other words — each class has a private set of private variables that no-one else has access to.

A sample demonstrating the percularities of private methods when extending classes:

abstract class base <
public function inherited () <
$this -> overridden ();
>
private function overridden () <
echo ‘base’ ;
>
>

class child extends base <
private function overridden () <
echo ‘child’ ;
>
>

$test = new child ();
$test -> inherited ();
?>

Output will be «base».

If you want the inherited methods to use overridden functionality in extended classes but public sounds too loose, use protected. That’s what it is for:)

A sample that works as intended:

abstract class base <
public function inherited () <
$this -> overridden ();
>
protected function overridden () <
echo ‘base’ ;
>
>

class child extends base <
protected function overridden () <
echo ‘child’ ;
>
>

$test = new child ();
$test -> inherited ();
?>
Output will be «child».

Источник

Php class this scope

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

    Источник

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