php类的使用,5.PHP类的基础使用

1.class的基本使用<?php

// 关键词:访问控制符,public ,protected,private

// 1.public(公有):公有的类成员可以在任何地方被访问

// 2.protected(受保护):受保护的类成员则可以被其自身以及其子类和父类访问

// 3.private(私有):私有的类成员则只能被其定义所在的类访问

class Demo00

{

// 定义属性

public $name;       // 名称

protected $position;   // 职位

private $salary;       // 工资

protected $department; // 部门

// __construct():构造方法,当类实例化时自动执行

// 用构造方法传参数进来给属性们

public function __construct($name,$position,$salary,$department)

{

$this->name = $name;

$this->position = $position;

$this->salary = $salary;

$this->department = $department;

}

public function getPosition()

{

return $this->department === '培训部' ? $this->position : '无权查看';

}

public function getSalary()

{

return $this->department === '财务部' ? $this->salary : '无权查看';

}

}

// 实例化对象,把参数传给我构造方法

$obj = new Demo00('Jerry','教师',5555,'培训部');

// echo $obj->getPosition();

// echo $obj->position;

class Sub extends Demo00{}

$obj = new Sub('Tom','高级讲师',5656,'财务部');

echo $obj->getSalary();

2.类的静态属性与静态方法<?php

class Demo00

{

public $product;      // 产品

public static $price; // 价格 (静态)

public function __construct($product,$price)

{

$this->product = $product;

self::$price = $price;

}

// 对象方法

public function getInfo1()

{

// 这个方法可以在对象中访问,方法中使用了静态属性,可以在对象***享

return $this->product.'价格是: '.self::$price.'元';

}

// 公有静态方法

public static function getInfo2()

{

// 此时方法是公有的静态方法,里面不能存在对象$this方式来调用,会报错

// return $this->product.'价格是: '.self::$price.'元';

}

public static function getInfo3($product)

{

return $product.'价格是: '.self::$price.'元';

}

}

$obj = new Demo00('衣服',300);

// echo $obj->product;

// echo Demo00::$price;

// echo $obj->getInfo1().'
';

// echo $obj->getInfo2().'
'; // 不能通过对象调用静态方法,会报错

// echo Demo00::getInfo3($obj->product);

// echo $obj->price; // 对象不能访问静态属性,会报错

// echo $obj->getInfo3($obj->product); // 对象可以访问静态方法 ok

3.类常量<?php

// 类常量

class Demo00

{

// 类常量与类属性一样,也是属于类的,必须用类访问,不能用对象访问

const NATION = '中国';

public static $sex = '男';

private $name;

public function __construct($name)

{

$this->name = $name;

}

public function getInfo()

{

return $this->name.'性别是: '.self::$sex.',国际: '.self::NATION;

}

}

$obj = new Demo00('Tom');

// echo Demo00::NATION,'
'; // 访问类常量

// echo Demo00::$sex;          // 访问类属性

// echo $obj->getInfo(); // 改方法访问了类属性与类常量

Demo00::$sex = '保密'; // 修改类属性

// Demo00::NATION = '美国'; // 报错 类常量不能修改

echo $obj->getInfo();

4.属性重载<?php

// 重载

class Demo00

{

private $name;

private $salary;

protected $secret = '飞流直下三千尺';

public function __construct($name,$salary)

{

$this->name = $name;

$this->salary = $salary;

}

// __get($name):当获取一个未定义或 私有 受保护 的属性是触发

public function __get($name)

{

if($name == 'secret'){

return ($this->name == 'admin') ? $this->$name : '无权查看';

}

return $this->$name;

}

// __set($name,$value):当设置未定义或 私有 受保护 的属性赋值时触发

public function __set($name,$value)

{

// if($name == 'salary'){

// return ($this->name == 'admin') ? $this->$name = $value : '无权更新';

// }

return $this->$name = $value;

}

}

$obj = new Demo00('admin',6666);

// echo $obj->name;

// echo $obj->salary;

// echo $obj->secret;

$obj->salary = 10000;

echo $obj->salary;

5.方法重载<?php

// 方法重载

class Demo00

{

// __call($method,$arguments):访问不存在,私有,受保护的方法时触发

public function __call($method,$arguments)

{

return '方法名:'.$method.'
方法参数

'.print_r($arguments,true);

}

// __callStatic($method):访问不存在,不可见(静态)的方法时触发

public static function __callStatic($method,$arguments)

{

return '方法名:'.$method.'
方法参数

'.print_r($arguments,true);

}

}

$obj = new Demo00();

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

echo Demo00::getInfo2('html','css','php','java');

echo '


';

function sum($a,$b){

return $a.' + '.$b.' = '.($a+$b);

}

// 把一个参数作为回调函数调用

echo call_user_func('sum',40,50).'
';

// 调用一个回调函数,并把数组参数作为回调函数参数

echo call_user_func_array('sum',[60,60]).'
';

echo '


';

class Test1

{

public function sum($a,$b)

{

return $a.' + '.$b.' = '.($a+$b);

}

public function sum2($a,$b)

{

return $a.' * '.$b.' = '.($a*$b);

}

}

$obj = new Test1();

echo call_user_func([$obj,'sum'],60,20).'
';

echo call_user_func_array([new Test1(),'sum2'],[10,10]);

echo '


';

class Test2

{

public static function num($a,$b)

{

return $a.' * '.$b.' = '.($a*$b);

}

}

echo call_user_func('Test2::num',2,5).'
';

echo call_user_func_array(['Test2','num'],[2,6]).'
';

echo '类名是: '.Test2::class.'
';

echo call_user_func_array([Test2::class,'num'],[2,9]);

6.数据库操作

Query.php<?php

class Query

{

public $pdo = null;

public $table;

public $field;

public $where;

public $limit;

public function __construct($pdo)

{

$this->pdo = $pdo;

}

public function table($table)

{

$this->table = $table;

return $this;

}

public function field($field)

{

$this->field = $field;

return $this;

}

public function where($where)

{

$this->where = $where;

return $this;

}

public function limit($limit)

{

$this->limit = $limit;

return $this;

}

public function select()

{

$field = empty($this->field) ? '*' : $this->field;

$where = empty($this->where) ? ''  : $this->where;

$limit = empty($this->limit) ? ''  : $this->limit;

// 构造sql查询语句

$sql = "SELECT ".$field." FROM ".$this->table.' WHERE '.$where.' LIMIT '.$limit;

$stmt = $this->pdo->prepare($sql);

$stmt->execute();

return $stmt->fetchAll(PDO::FETCH_ASSOC);

}

}<?php

require "./Query.php"; // 引入数据库操作类

class Db

{

protected static $pdo = null;

public static function connect()

{

self::$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');

}

public static function __callStatic($tbName,$arguments)

{

self::connect();

$query = new Query(self::$pdo);

return call_user_func_array([$query,$tbName],[$arguments[0]]);

}

}

$staffs = Db::table('staff')->field('name,course')->where('id>5')->limit('0,5')->select();

echo '

';print_r($staffs);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值