类与对象-属性
<?php
header("Content-type:text/html; charset=utf-8");//@date: 2013-02-23
//類與對象-屬性
//屬性的聲明是由關鍵之子public或者protected或者private開頭,然后跟一個變量來組成,屬性中的變量可以初始化,但是初始化的值必須是常數,這里的常數是指php腳本在編譯階段時就為常數,而不是在編輯階段之后在運行階段運算出來的常數// abstract class PropertyObject
// {
// public function __get($name)
// {
// if(method_exists($this, ($method='get_'.$name))){
// return $this->$method();
// }else
// return;
// }// public function __isset($name)
// {
// if(method_exists($this, ($method ='isset_'.$name)))
// {
// return $this->$method();
// }else
// return;
// }// public function __set($name, $value)
// {
// if(method_exists($this, ($method='set_'.$name)))
// {
// $this->$method($value);
// }
// }// public function __unset($name)
// {
// if(method_exists($this, ($method='unset_'.$name)))
// {
// $this->$method();
// }
// }
// }// class test
// {
// public $var1 = 1;
// protected $var2 = 2;
// private $var3 = 4;
// static $var4 = 4;// public function toArray()
// {
// return (array)$this;
// }
// }
// $t =new test;
// print_r($t->toArray());//類常量
// class MyClass
// {
// const constant = 'constant value';// function showConstant()
// {
// echo self::constant."<BR>";
// }
// }
// echo Myclass::constant;// $classname = "MyClass";
// echo $classname::constant; //PHP5.3之后// $class = new MyClass(); //PHP5.3之后
// $class->showConstant();
// echo $class::constant;// class Weather
// {
// const danger = 'parent';// static function getDanger($class)
// {// }
// }
// class Rain extends Weather
// {
// const danger = 'child';
// }
// $obj = new Rain();
// $danger = constant($class::danger);// abstract class dbObject
// {
// const TABLE_NAME = 'undefined';// public static function GetAll()
// {
// //Call to undefined function get_called_class()
// //提示錯誤,無語了
// $c = get_called_class(); //get_called_class() 獲取靜態方法調用的類名
// //return "SELECT * FROM ".$c::TABLE_NAME;
// //return "SELECT * FROM `".$c::TABLE_NAME."`";
// return "SELECT * FROM ";
// }
// }
// class dbPerson extends dbObject
// {
// const TABLE_NAME = 'persons';
// }
// class dbAdmin extends dbPerson
// {
// const TABLE_NAME = 'admins';
// }
// echo dbPerson::GetAll()."<BR><BR>";
//手冊上這個例子也一樣,應該是PHP版本問題//constant()函數返回常量的值
// function get_class_const($class, $const)
// {
// return constant(sprintf('%s::%s', $class, $const));
// }
// class Foo
// {
// const BAR = 'foobar';
// }
// $class = "Foo";
// echo get_class_const($class, 'BAR');// abstract class a
// {
// private function readConst()
// {
// return $this->getConst('CONST');
// }
// //兩個抽象類
// abstract public static function getConstFromOutside($const);
// abstract protected function getConst($const);
// }// class b extends a
// {
// private static $CONST = 'any value';// public static function getConstFromOutside($const)
// {
// return self::$$const;
// }
// protected function getConst($const)
// {
// self::$$const;
// }
// }
// echo b::getConstFromOutside('CONST');//eval()函數把字符串按照PHP代碼來計算
//該字符串必須是合法的PHP代碼,且必須以分號結尾
//如果沒有在代碼字符串中調用return語句,則返回NULL,如果代碼中存在解析錯誤,則eval()函數返回false// class Test
// {
// public static $open=2;
// protected static $var = 1;
// private static $secret = 3;
// }
// $classname = "Test";
// //什么是php反射類,顧名思義,可以理解為一個類的映射。
// $x = new ReflectionClass($classname);
// $y = array();
// //GetStaticProperties()取得所有靜態的方法
// foreach($x->GetStaticProperties() as $k=>$v)
// {
// //echo str_replace(chr(0), "@", $k);
// //echo $k;
// $y[str_replace(chr(0), "@", $k)] = $v;
// //$y[$k] = $v;
// }
// var_dump($y);
// $a = array("open", "var", "secret", "nothing");
// foreach($a as $b)
// {
// if(isset($y["$b"]))
// echo "\$b is public: {$y[$b]}<BR><BR>";
// elseif(isset($y["@*@$b"]))
// echo "\"$b\" is protected: {$y["@*@$b"]}<br/>";
// else
// echo "\$b:{$b} is not a static member of $classname<BR>";
// }// class A
// {
// const X = 1;
// const Y = self::X;
// }
// class B extends A
// {
// const X = 1.0;
// }
// var_dump(B::Y);
// echo A::Y;//final用于一個類不能被繼承,或者一個方法不能被覆蓋
// abstract class aClassConstant
// {
// final function __set($member, $value){
// throw new Exception("你不能設置一個常數!");
// }// final function __get($member)
// {
// return $this->$member;
// }
// }// class DbConstant extends aClassConstant
// {
// protected $host = 'localhost';
// protected $user = 'user';
// protected $password = 'pass';
// protected $database = 'db';
// protected $time;// function __construct()
// {
// $this->time = time() +1;
// }
// }
// $dbConstant = new DbConstant();
// echo $dbConstant->host;
// $dbConstant->host = '127.0.0.1';//const FOO = 'bar'; //這句有錯
//var_dump(FOO);// class Foo
// {
// const a=7;
// const x = 99;
// }
// class Bar extends Foo
// {
// const a = 42;
// }
// $b = new Bar();
// $r = new ReflectionClass($b); //
// echo $r->getConstant('a');//42
// echo "<BR>".$r->getConstant('x'); //99// class Foo
// {
// const foo = 'bar';
// public $foo = 'foobar';// const bar = 'foo';
// static $bar = 'foobar';
// }
// var_dump(foo::$bar); //foobar
// echo "<BR>";
// var_dump(foo::bar); //foo
// echo "<BR>";// $bar = new Foo();
// var_dump($bar->foo); //foobar
// echo "<BR>";
// var_dump(bar::foo); //foo(現在的版本不支持)// interface AppConstants
// {
// const FOOBAR = 'Hello, World';
// }
// class Example implements AppConstants
// {
// public function test()
// {
// echo self::FOOBAR;
// }
// }
// $obj = new Example();
// $obj->test();// class Dimension
// {
// const MIN = 0, MAX=800;// public $width, $height;// public function __construct($w=0, $h=0)
// {
// $this->width = self::clamp($w);
// $this->height = self::clamp($h);
// }// public function __toString()
// {
// return "數據為: [width={$this->width}, height={$this->height}]";
// }// protected static function clamp($value)
// {
// if($value < self::MIN) $value = self::MIN;
// if($value > self::MAX) $value = self::MAX;
// return $value;
// }
// }
// echo (new Dimension())."<BR>";
// echo (new Dimension(1500,97))."<BR>";
// echo (new Dimension(14, -20))."<BR>";
// echo (new Dimension(240, 80))."<BR>";// class abc
// {
// const avar = "abc's";
// function show()
// {
// echo self::avar."<BR><BR>";
// }
// }
// class def extends abc
// {
// const avar = "def's";
// function showmore()
// {
// echo self::avar."<BR><BR>";
// $this->show();
// }
// }
// $bob = new def();
// $bob->showmore();
//def's
//abc's// class abc
// {
// protected $avar = "abc's";
// function show()
// {
// echo $this->avar."<BR><BR>";
// }
// }
// class def extends abc
// {
// protected $avar = "def's";
// function showmore()
// {
// echo $this->avar."<BR><BR>";
// $this->show();
// }
// }
// $bob = new def();
// $bob->showmore();
?>
轉載于:https://www.cnblogs.com/xiangxiaodong/archive/2013/02/24/2926854.html
總結
- 上一篇: zigbee 端点描述符
- 下一篇: LA 4328 Priest John'