设为首页 收藏本站
查看: 860|回复: 0

[经验分享] PHP实践之路(九)类与对象(2)

[复制链接]

尚未签到

发表于 2017-4-9 13:06:27 | 显示全部楼层 |阅读模式
  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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362442-1-1.html 上篇帖子: php多维数组排序以及实际工作中的应用 下篇帖子: 新版本php 5.3安装中short_tag的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表