note 100611 added to language.oop5.late-static-bindings
| From: | tfn dot yldrm at hotmail dot com | Date: | Tue, 26 Oct 2010 19:30:45 +0000 |
| Subject: | note 100611 added to language.oop5.late-static-bindings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173479@lists.php.net to get a copy of this message | ||
example for static binding on 5.2 and real enumaration
<?php
/**
* Static Binding On PHP 5.2
* @author Tufan Baris YILDIRIM
* @since 26.10.2010
*/
abstract class EnumBase
{
protected $num = 0;
public $toString;
public $toInt;
public function __construct($enumKeyOrVal)
{
unset($this->toString,$this->toInt);
$enums = $this->enums();
if(
empty($enumKeyOrVal)
||
!(isset($enums[$this->num = $enumKeyOrVal])
||
($this->num = array_search($enumKeyOrVal,$enums)) !== false)
)
$this->num = 0;
/**
* 5.3 Version
*/
/*
* if(
empty($enumKeyOrVal)
||
!(isset(static::$enums[$this->num = $enumKeyOrVal])
||
($this->num = array_search($enumKeyOrVal,static::$enums)) !== false)
)
$this->num = 0;
*/
}
#5.3 e geçilirse gerek kalmaz.
public function vars()
{
return get_class_vars(get_class($this));
}
public function enums()
{
$vars = $this->vars();
return $vars['enums'];
}
public function __get($property)
{
if(method_exists($this,'__'.$property))
return $this->{'__'.$property}();
else
return $this->__toString();
}
public function __toInt()
{
return $this->num;
}
public function __toString()
{
$enums = $this->enums();
if(isset($enums[$this->num]))
{
return $enums[$this->num];
}
else
{
return $enums[0];
}
/**
* 5.3 Version
*/
/*
if(isset(static::$enums[$this->num]))
{
return static::$enums[$this->num];
}
else
{
return static::$enums[0];
}
*/
}
}
class Positions extends EnumBase
{
public static $enums = array(
0 => 'Bilinmiyor',
1 => 'Kale',
2 => 'Defans',
3 => 'Orta Saha',
4 => 'Forvet'
);
}
$a = new Positions('Orta Saha');
$b = new Positions(4);
$c = (string)$a; // Orta Saha
$d = (string)$b; // Forvet
?>
----
Server IP: 69.147.83.197
Probable Submitter: 95.10.5.89
----
Manual Page -- http://www.php.net/manual/en/language.oop5.late-static-bindings.php
Edit -- https://master.php.net/note/edit/100611
Del: integrated -- https://master.php.net/note/delete/100611/integrated
Del: useless -- https://master.php.net/note/delete/100611/useless
Del: bad code -- https://master.php.net/note/delete/100611/bad+code
Del: spam -- https://master.php.net/note/delete/100611/spam
Del: non-english -- https://master.php.net/note/delete/100611/non-english
Del: in docs -- https://master.php.net/note/delete/100611/in+docs
Del: other reasons-- https://master.php.net/note/delete/100611
Reject -- https://master.php.net/note/reject/100611
Search -- https://master.php.net/manage/user-notes.php