1. PHP类属性定义和访问方式:
<?php
class testClass {
const tConst = 1;
public $tVar = 2; //或 public $tVar; 前面需要有变量修饰符
static $tSta = 3;
public function __construct(){
echo $this->tConst; //Undefined property: testClass::$tConst
echo self::tConst; //正确输出 1
echo $this->tVar; // 正确输出 2【注意tVar前不能有$符号】
echo self::$tVar; // Access to undeclared static property: testClass::$tVar
echo self::tVar; // tVar前需要加上$符号,Undefined class constant 'tVar'
echo $this->tSta; //Undefined property: testClass::$tSta
echo self::$tSta; //正确输出 3
}
}
$testClass = new testClass();
?>
总结几点:
在实例化对象时 $testClass = new testClass(); 其中testClass()中的()可以省略,当构造函数有显式声明需要参数时,需要在这里传入参数
如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::
如果被引用的变量或者方法没有被声明成const或者static,那么就必须使用操作符->
从类的内部访问const或者static变量或者方法,使用自引用的self
从类的内部访问不为const或者static变量或者方法,使用自引用的$this
2. $this-> 究竟指向哪?
<?php
class testClass {
var $nick = '';
public function __construct($nick){
$this->nick = $nick;
}
public function display(){
echo $this->nick;
}
}
$testClass1 = new testClass('frank');
$testClass1->display(); //echo $testClass1->nick; 和此结果相同
?>
$this->display(); 其实就是 $testClass1->nick,因此$this究竟指向哪是由所实例化的对象决定的,指向当前对象实例的指针。包括变量、方法都是如此
$this->方法() 的例子:
<?php
class baseClass{
public function testFunc(){
echo "\n" . 'I`m boss';
}
}
class parentClass extends baseClass{
public function testFunc(){
echo "\n" . 'I`m the up';
}
}
class testClass extends parentClass{
var $nick = '';
public function __construct($nick){
$this->nick = $nick;
}
public function display(){
echo $this->nick;
$this->testFunc();
}
}
$testClass1 = new testClass('frank');
$testClass1->display();
?>
这样的代码最后的输出结果是什么呢?关键是看testFunc()方法。
如果在类中用$this调用一个当前类中不存在的方法或变量,它会依次去父类寻找,直到找不到再报错
基于第一条,如果找到了需要的方法或变量,就会停止寻找,如果其上级父类中还有同样的,则选择当前找到的
所以上面代码输出为:
frank
I`m the up
3. 关于parent::
parent是对父类的引用
<?php
class A {
public $a = 'aa';
public function callFunc(){
echo 'parent';
}
}
class B extends A{
public function __construct(){
parent::callFunc();
echo "\n" . $this->a;
}
}
$oB = new B;
?>
在class B中,若要调用其父类A中的callFunc()方法,就需要使用parent::callFunc(),但A类中此方法必须是public修饰的,否则会提示 Fatal error: Call to private method A::callfunc() from context 'B'...
另外需要注意的是,对于父类中的属性$a,调用的时候用$this,用parent就访问不到了。若$a在A类中是这样定义的:public static $a,则访问的时候就需要parent::$a
注意:parent不具备传递性,它只能代表当前类的父类,若此例子A类继承base类,在base类中定义baseFunc()方法,那么在B类中使用parent::baseFunc()是错误的,只能在A类中这样使用。
4. self::又指向哪里?
简单的说,self和某具体实例没有关系,它只指向当前类,不会受某具体类对象的影响
<?php
class testClass{
public static $count = 0;
public $num = 0;
function __construct(){
self::$count++;
$this->num++;
}
function display(){
echo self::$count . "\n";
echo $this->num . "\n";
}
}
$oTest1 = new testClass;
$oTest1->display(); //输出: 1 1
$oTest2 = new testClass;
$oTest2->display(); //输出: 2 1
?>
上面例子中self::$cout始终指向该类本身,而$num是单独存在于各个具体实例中的。
5. 总结:
$this 指向当前的实例
$parent 是父类的引用
self 对当前类本身的一个引用