在访问php类中的成员变量或方法时,如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::,反之如果被引用的变量或者方法没有被声明成const或者static,那么就必须使用操作符->.
另外,如果从类的内部访问const或者static变量或者方法,那么就必须使用自引用的self,反之如果从类的内部访问不为const或者static变量或者方法,那么就必须使用自引用的$this.
$this实例代码如下:
content = $content;
}
function __destruct() {
} //定义析构函数
function printContent() { //定义打印函数
echo $this->content . '
';
}
}
$test = new test_this('北京欢迎你!'); //实例化对象
$test->printContent(); //北京欢迎你!
?>::使用方法实例代码如下:
name = $name;
}
}
class test_son extends test_parent { //派生类 继承test_parent
public $gender; //定义性别
public $age; //定义年龄
function __construct($gender, $age) { //继承类的构造函数
parent::__construct('nostop'); //使用parent调用父类的构造函数,来进行对父类的实例化
$this->gender = $gender;
$this->age = $age;
}
function __destruct() {
}
function print_info() {
echo $this->name . '是个' . $this->gender . ',今年' . $this->age . '岁' . '
';
}
}
$nostop = new test_son('女性', '22'); //实例化test_son对象
$nostop->print_info(); //执行输出函数 nostop是个女性,今年23岁
?>使用self::$name的形式.注意的是const属性的申明格式,const PI=3.14,而不是const $PI=3.14
立即学习“PHP免费学习笔记(深入)”;
实例代码如下:
UQCMS云商是一款B2B2C电子商务软件 ,非常适合初创的创业者,个人及中小型企业。程序采用PHP+MYSQL,模板采用smarty模板,二次开发,简单方便,无需学习其他框架就可以自行模板设计。永久免费使用,操作简单,安全稳定。支持PC+WAP+微信三种浏览方式,支持微信公众号。
还要注意的一点是如果类的方法是static的,他所访问的属性也必须是static的.
在类的内部方法访问未声明为const及static的属性时,使用$this->value ='class_a';的形式.
文章地址:
转载随意^^请带上本文地址!










