php作业案例10,实例演示10月8号作业-19年10月8日

属性重载

实例

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 '
', Demo1::test();

echo '
', $obj->test();

运行实例 »

点击 "运行实例" 按钮查看在线实例

回调函数

实例

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 '
';

$obj->salary =9999;

echo $obj->salary;

// echo $obj->name, '
';

// $obj->salary = 2;

// echo $obj->salary;

// echo '


';

// echo isset ( $obj->salary) ? '存在' :'不存在';

// unset($obj->salary);

// echo $obj->salary;

运行实例 »

点击 "运行实例" 按钮查看在线实例

方法重载

实例

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 '
';

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 '
';

echo call_user_func_array([$obj,'sum'],[20,20]);

class test3{

public static function sum($a,$b)

{

return "{$a}/{$b} =" .($a/$b);

}

}

echo '
';

echo call_user_func_array([test3::class,'sum'],[20,20]);

class demo3

{

public function __call($name,$args){

return '方法是:'.$name.'
参数列表:

'.print_r($args, true);

}

public static function __callstatic($name,$args){

return '方法是:'.$name.'
参数列表:

'.print_r($args, true);

}

}

echo '
';

$obj =new demo3();

echo $obj->getinfo1(10,20,30);

echo demo3::getinfo2('html','css','js');

运行实例 »

点击 "运行实例" 按钮查看在线实例

数据库链接调用 demo4.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

实例

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'
';

}

运行实例 »

点击 "运行实例" 按钮查看在线实例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值