get_called_class--后期静态绑定("Late Static Binding")类的名称
2016-02-03 18:00
429 查看
get_called_class--后期静态绑定("Late Static Binding")类的名称
string get_called_class ( void )
获取静态方法调用的类名。
返回类的名称,如果不是在类中调用则返回 FALSE。
<?php
class Person{
public $username;
public $age;
public $height;
public $weight;
static public $number = 0;
public function __construct($username,$age,$height,$weight){
$this->username = $username;
$this->age = $age;
$this->height = $height;
$this->weight = $weight;
self::$number++;
}
public function __set($name,$value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
/**
* 1)static方法中不能直接使用非静态成员,因为非静态成员与实例相关,通过实例化间接使用
* 2)static方法中不能用this(与实例相关)
* 3)非static方法中可以使用static成员
*/
static public function getUsernumber(){
var_dump(get_called_class());
return self::$number;
}
public function getUsername(){
var_dump(get_called_class());
return $this->username;
}
public function __toString(){
return '';
}
}
$p_person1 = new Person('zhaofei',23,185,62);
echo $p_person1->getUsername();
$p_person2 = new Person('zhaofei1',24,165,68);
$p_person3 = new Person('zhaofei2',23,180,82);
$p_person4 = new Person('zhaofei3',22,175,72);
echo Person::getUsernumber();?>
string get_called_class ( void )
获取静态方法调用的类名。
返回类的名称,如果不是在类中调用则返回 FALSE。
<?php
class Person{
public $username;
public $age;
public $height;
public $weight;
static public $number = 0;
public function __construct($username,$age,$height,$weight){
$this->username = $username;
$this->age = $age;
$this->height = $height;
$this->weight = $weight;
self::$number++;
}
public function __set($name,$value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
/**
* 1)static方法中不能直接使用非静态成员,因为非静态成员与实例相关,通过实例化间接使用
* 2)static方法中不能用this(与实例相关)
* 3)非static方法中可以使用static成员
*/
static public function getUsernumber(){
var_dump(get_called_class());
return self::$number;
}
public function getUsername(){
var_dump(get_called_class());
return $this->username;
}
public function __toString(){
return '';
}
}
$p_person1 = new Person('zhaofei',23,185,62);
echo $p_person1->getUsername();
$p_person2 = new Person('zhaofei1',24,165,68);
$p_person3 = new Person('zhaofei2',23,180,82);
$p_person4 = new Person('zhaofei3',22,175,72);
echo Person::getUsernumber();?>
相关文章推荐
- Android Jni Android.mk常用语句
- 配置MySQL GTID 主从复制
- JVM源码分析之Attach机制实现完全解读
- hibernate之insert和update控制实战
- iOS控件之UIResponder
- 收藏2
- [CodeForces #80 Div 1 D] 分块+树状数组/线段树
- Java项目开发环境构建工具 Gradle 使用笔记(简单、基本)
- 使用LS2J技术在LotusScript中使用Java
- class_exists — 检查类是否已定义
- JVM源码分析之堆外内存完全解读
- 进制转换 2
- 进制转换 2
- 规范
- 收藏1
- 【UOJ#171】【WC2016】挑战NPC【带花树开花】
- Android FFmpeg
- Connection for controluser as defined in your configuration failed.
- Java数据类型转换
- 使用LotusScript操作Lotus Notes RTF域