资源描述
静态延迟绑定,static
问题:
1:$this 永远代表所在类的对象? 不是
2:self 永远代码所在类吗? 是
如:
class P { public static $where = 'in P static preperty <br />'; public static function sayWhere() { echo self::$where; } } class C extends P { public static $where = ' in C static preperty <br />'; } echo P::sayWhere(); echo C::sayWhere();
输出结果为:
in P static preperty in P static preperty
显然,当前的逻辑不是十分完善
此时,应该是表示当前类的关键字,最好应该在调用时决定最好(self 办不到)
此时,采用一个新的关键字,代表当前类,与 selft 不同,在于是 运行时调用房室,而不在类编译时就确定好了的!
关键字是:static
示例:
class P { public static $where = 'in P static preperty <br />'; public static function sayWhere() { echo self::$where; } public static function sayW() { echo static::$where; } } class C extends P { public static $where = ' in C static preperty <br />'; } echo P::sayWhere(); echo C::sayWhere(); echo '<hr />'; echo P::sayW(); echo C::sayW();
输出结果为:
in P static preperty in P static preperty in P static preperty in C static preperty
总结:
static关键字的功能:
1:静态局部变量
2:声明静态成员
3:当前类,运行时的当前类
类中,可以表示类的关键字:
self,所在类
static,调用类
parent,父类
参数的类型约束
约束函数,或者方法类参数的类型,只能是某个类的对象
PHP是弱类型:变量可以存储任意类型的数据!
函数,方法的参数也是可以接收任意类型
但是参数,可以被规定为,某个类的固定对象
示例:
class Student { public $stu_name; } $s = new Student; $s->stu_name = 'Bruce Lee'; function sayName(Student $o) { echo 'Hello , ',$o->stu_name; } sayName('ginvip');
输出结果为:
Catchable fatal error: Argument 1 passed to sayName() must be an instance of Student, string given, called in E:\VIP\91.php on line 14 and defined in E:\VIP\91.php on line 10
在参数前增加类名即可!注意:只支持类名与数组
对象的遍历
对象是一个集合数据类型
简单的遍历,foreach()
foreach() 也可以遍历对象
遍历对象,是依次获得对象拥有的属性的信息
class Student { public $stu_name; public $stu_age; public $stu_gender; } $o = new Student; $o->stu_name = 'Bruce Lee'; $o->stu_age = 50; $o->stu_gender = 'male'; foreach($o as $p_name => $p_value) { var_dump($p_name,$p_value); echo '<br />'; }
留意,访问修饰可以影响
如:
class Student { public $stu_name; public $stu_age; private $stu_gender = 'secret'; } $o = new Student; $o->stu_name = 'Bruce Lee'; $o->stu_age = 50; //$o->stu_gender = 'male'; foreach($o as $p_name => $p_value) { var_dump($p_name,$p_value); echo '<br />'; }
输出结果为:
string(8) "stu_name" string(9) "Bruce Lee" string(7) "stu_age" int(50)
自定义遍历
场景:在遍历班级时,就相当于,遍历班级的所有学生,而不是班级的所有属性
/** * 班级类 */ class Team { public $name; public $stu_count; public $stu_infos; } $t = new Team; $t->name = 'PHP2026'; $t->stu_count = 3; $t->infos = array( array('id'=>'10','name'=>'Bruce Lee'), array('id'=>'15','name'=>'Jet Lee'), array('id'=>'34','name'=>'Gin Lee') ); //遍历班级 foreach($t as $p_name => $p_value) { }
此时,需要使用接口编程实现
foreach 时,php 会去判断当前所遍历的对象的类,是否类实现一个叫 iterator(迭代器) 接口
不是,就会依次得到对象的每个属性
是的话,会在 foreach 执行时,通过调用接口内规定的各个方法来完成这个遍历
两个问题:
foreach 的流程:
iterator 接口的内容:预定义的!PHP定义好的
显然,PHP的foreach 的五个重要的步骤,依赖于接口中定义的五个方法
编程实现,只需要遍历学生信息
1:类要实现 iterator 接口
/** * 班级类 */ class Team implements Iterator{ public $name; public $stu_count; public $stu_infos; }
2:依次完善抽象方法
遍历代码如下:
/** * 班级类 */ class Team implements Iterator{ public $name; public $stu_count; public $stu_infos; public function rewind() { reset($this->stu_infos); } public function valid() { return key($this->stu_infos) !== null; } public function current() { return current($this->stu_infos); } public function key() { return key($this->stu_infos); } public function next() { next($this->stu_infos); } } $t = new Team; $t->name = 'PHP2026'; $t->stu_count = 3; $t->stu_infos = array( array('id'=>'10','name'=>'Bruce Lee'), array('id'=>'15','name'=>'Jet Lee'), array('id'=>'34','name'=>'Gin Lee') ); //遍历班级 foreach($t as $p_name => $p_value) { var_dump($p_name,$p_value); echo '<br />'; }
常用的对象,类函数
is_object();
class_exists();
interface_exists();
get_class();
get_parent_class();
get_class_vars(); 得到类的变量(属性)
get_class_methods(); 得到类的方法
class T { public $s_name; public $s_age = 20; public static $stu_count = 0; public function f1() { } public static function f2() { } private function f3() { } } var_dump(get_class_vars('T')); echo '<hr />'; var_dump(get_class_methods('T'));
get_declared_classes(); 得到所有已经定义的类
var_dump(get_declared_classes());
可见有自定义类,和预定义类
stdClass,内置的标准类
$arr = array('name'=>'wang','age'=>10); $obj = (object)$arr; var_dump($obj);
输出结果为:
object(stdClass)#1 (2) { ["name"]=> string(4) "wang" ["age"]=> int(10) }
魔术常量
__CLASS__,当前类名(self 可以 new ,__CLASS__ 不可以 new)
__METHOD__,当前方法名。区别 __FUNCTION__
class Student { public function sayHello() { echo __CLASS__; echo '<br />'; echo __METHOD__; echo '<br />'; echo __FUNCTION__; } } $o = new Student; $o->sayHello();
输出结果为:
Student Student::sayHello sayHello
面向对象的特征:
封装
继承
多态
展开阅读全文