PHP实践之路(九)类与对象(2)
PHP实践之路(目录索引)类与对象(2)
1、构造函数
a、构造函数的作用主要是初始化对象信息,每次创建新对象时都会调用构造函数
b、构造函数模式为
void __construct($args)
c、为了实现向后兼容性,如果 PHP 5 在类中找不到 __construct() 函数,它就会尝试寻找旧式的构造函数,也就是和类同名的函数
d、如果子类中定义了构造函数则不会隐式调用其父类的构造函数。要执行父类的构造函数,需要在子类的构造函数中调用
parent::__construct()。子类和父类的说法是在继承中有的,继承我们将在后面讨论(这个实验我们将在继承中体现)
我们来举个例子
<?php
//换行输出
function println($var){
echo $var;
echo "<br />";
}
//账户
class Account{
private $username;
private $password;
//构造函数
function __construct($name,$pass){
println("execute construct...");
$this->username=$name;
$this->password=$pass;
}
function printInfo(){
println("username:".$this->username);
println("password:".$this->password);
}
}
$account=new Account("cyxl","123");//输出:execute construct...
$account->printInfo();
/**输出
username:cyxl
password:123
*/
class Account2{
private $username;
private $password;
//旧式同名构造函数
function Account2($name,$pass){
println("execute construct...");
$this->username=$name;
$this->password=$pass;
}
function printInfo(){
println("username:".$this->username);
println("password:".$this->password);
}
}
$account=new Account2("jack","123");//输出:execute construct...
$account->printInfo();
/**输出
username:jack
password:123
*/
?>
2、析构函数
a、析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行(自PHP5开始引入析构函数)
b、父类的析构函数不会被引擎暗中调用。要执行父类的析构函数,必须在子类的析构函数体中显式调用 parent::__destruct()。(这个实验我们将在继承中体现)
c、析构函数即使在使用exit()终止脚本运行时也会被调用。在析构函数中 调用exit()将会中止其余关闭操作的运行
实验时间
<?php
//换行输出
function println($var){
echo $var;
echo "<br />";
}
//账户
class Account{
private $username;
private $password;
//构造函数
function __construct($name,$pass){
println("execute construct...");
$this->username=$name;
$this->password=$pass;
}
function printInfo(){
println("username:".$this->username);
println("password:".$this->password);
}
function __destruct(){
println("execute destruct...");
}
}
$account=new Account("cyxl","123");//输出:execute construct...
///exit();//如果此处调用exit()函数,脚本将终止执行,但对象的析构函数还是会执行输出:execute destruct...
$account->printInfo();
/**输出
username:cyxl
password:123
*/
//脚本执行完后,系统自动调用析构函数输出:execute destruct...
?>
3、继承
a、继承也就是子类继承自父类,子类将会继承父类的所有公有和保护方法,但是子类的方法会覆盖父类的方法。这个可以联系实际情况加以理解
b、一个子类只可以继承一个父类
c、使用final关键字修饰的类不能被继承,使用final修饰的方法不能被覆盖
好了,我们做做实验吧
<?php
//换行输出
function println($var){
echo $var;
echo "<br />";
}
class Father{
private $name;
protected $money;
public $friends;
//构造函数初始化对象
function __construct(){
println("father construct executed...");
$this->name="father";
$this->money=10000.0;//父亲的初始财富值,也许是他爸爸留给他的
$this->friends=array("jack","lily");
}
function work(){
println("father working..");
$this->money+=10;//父亲通过工作挣钱
}
function printInfo(){
println("name:".$this->name);
println("money:".$this->money);
echo "friends:";
foreach($this->friends as $friend){
echo $friend." ";
}
echo "<br />";
}
function __destruct(){
println("father gone");
}
}
class Sun extends Father{
private $name;
private $other;
function __construct(){
println("sun construct executed...");
parent::__construct();//如果没有这行显示的调用父类的构造函数,那么父类的构造函数将不会执行
$this->name="sun";//儿子自己的名字
$this->other="somthing else";
}
function work(){
println("sun working..");
$this->money+=20;//儿子继承了父亲的财产,通过自己挣钱使财富值增加了(儿子挣钱的能力比父亲强)
}
//儿子有自己的朋友
function makeFriends(){
$this->friends=array("mike","tom","bake");
}
function printInfo(){
println("name:".$this->name);
println("money:".$this->money);
println("other:".$this->other);
echo "friends:";
foreach($this->friends as $friend){
echo $friend." ";
}
echo "<br />";
}
function __destruct(){
parent::__destruct();
println("sun gone");
}
}
//定义父亲对象
$father=new Father();
$father->work();
$father->printInfo();
println("========================");
//定义儿子对象,很多属性都继承自父亲
$sun=new Sun();
$sun->printInfo();
println("========================");
$sun->work();
$sun->makeFriends();
$sun->printInfo();
println("========================");
//接下来是系统自动销毁对象
?>
以上程序的输出结果为
father construct executed...
father working..
name:father
money:10010
friends:jack lily
========================
sun construct executed...
father construct executed...
name:sun
money:10000
other:somthing else
friends:jack lily
========================
sun working..
name:sun
money:10020
other:somthing else
friends:mike tom bake
========================
father gone
sun gone
father gone
版权声明:本文为博主原创文章,未经博主允许不得转载。
页:
[1]