<?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(); ?>