属性重载
实例
<?php namespace _1008; class Demo1 { public $product; //变成静态 static public static $price; public function __construct($product, $price) { $this->product = $product; self::$price = $price; } public function test(){ return self::$price; } } $obj = new Demo1('电脑', 4890); echo '品名: '. $obj->product, ', 价格: ', Demo1::$price; //echo '<br>', Demo1::test(); echo '<br>', $obj->test();
运行实例 »
点击 "运行实例" 按钮查看在线实例
回调函数
实例
<?php namespace _1008; class Demo2 { private $name; private $salary; protected $secret= '1111111111'; public function __construct($name, $salary) { $this ->name = $name; $this ->salary = $salary; } public function __get($name) { if($name === 'secret'){ return ($this ->name ==='admin')? $this->$name : '无权查看'; } return $this->name; } public function __set($name, $value) { if($name==='salary'){ return $this->name === 'admin' ? $this->$name =$value :'无权更新'; } return $this->$name= $value; } public function __isset($name) { return isset($this ->$name); } public function __unset($name) { unset($this ->$name); } } $obj = new Demo2('admin', 6666); // echo $obj->secret; echo '<br>'; $obj->salary =9999; echo $obj->salary; // echo $obj->name, '<br>'; // $obj->salary = 2; // echo $obj->salary; // echo '<hr>'; // echo isset ( $obj->salary) ? '存在' :'不存在'; // unset($obj->salary); // echo $obj->salary;
运行实例 »
点击 "运行实例" 按钮查看在线实例
方法重载
实例
<?php namespace _1008; function sum($a,$b) { return "{$a}+{$b} =" .($a+$b); } echo call_user_func_array( __NAMESPACE__.'\sum',[50,30]); class test1{ public function sum($a,$b) { return "{$a}-{$b} =" .($a-$b); } } $obj= new test1(); echo '<br>'; echo call_user_func_array([$obj,'sum'],[40,20]); class test2{ public static function sum($a,$b) { return "{$a}*{$b} =" .($a*$b); } } $obj= new test2(); echo '<br>'; echo call_user_func_array([$obj,'sum'],[20,20]); class test3{ public static function sum($a,$b) { return "{$a}/{$b} =" .($a/$b); } } echo '<br>'; echo call_user_func_array([test3::class,'sum'],[20,20]); class demo3 { public function __call($name,$args){ return '方法是:'.$name.'<br>参数列表:<pre>'.print_r($args, true); } public static function __callstatic($name,$args){ return '方法是:'.$name.'<br>参数列表:<pre>'.print_r($args, true); } } echo '<br>'; $obj =new demo3(); echo $obj->getinfo1(10,20,30); echo demo3::getinfo2('html','css','js');
运行实例 »
点击 "运行实例" 按钮查看在线实例
数据库链接调用 demo4.php
实例
<?php namespace _1008; class query{ public $pdo = null; public $table; public $field; public $where; public $limit; public function __construct($pdo){ $this->pdo=$pdo; } // 设置数据表名称 public function table($tableName) { $this->table = $tableName; //返回当前类实例, 用来链式调用后面的其它方法 return $this; } // 设置数据表字段 public function field($fields = '*') { $this->field = empty($fields) ? '*' : $fields; return $this; } // 设置查询条件 public function where($where = '') { $this->where = empty($where) ? $where : ' WHERE '. $where; return $this; } // 设置显示数量 public function limit($limit) { $this->limit = empty($limit) ? $limit : ' LIMIT '. $limit; return $this; } public function select() { // SELECT * FROM table WHERE **** LIMIT n // 拼装SQL $sql = 'select ' . $this->field // 字段列表 . ' from ' . $this->table // 数据表 . $this->where // 条件 . $this->limit; // 显示数量 // 预处理 $stmt = $this->pdo->prepare($sql); $stmt->execute(); // die($stmt->debugDumpParams()); // 查看生成的sql return $stmt->fetchAll(\PDO::FETCH_ASSOC); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
demo5.php
实例
<?php namespace _1008; require 'demo4.php'; class db{ protected static $pdo = null; public static function connection(){ self::$pdo=new \pdo('mysql:host=127.0.0.1;dbname=php','root','root'); } public static function __callstatic($name, $arguments){ self::connection(); $query=new query(self::$pdo); return call_user_func_array([$query,$name],$arguments); } } $staffs=db::table('staff') ->field('staff_id,name,position, mobile') ->where('staff_id > 1') ->limit(1) ->select(); foreach ($staffs as $staff){ print_r($staffs); echo'<br>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例