博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP面向对象重要知识点----------第一部分
阅读量:7284 次
发布时间:2019-06-30

本文共 1089 字,大约阅读时间需要 3 分钟。

1. __construct: 

     内置构造函数,在对象被创建时自动调用。见如下代码:

arg1 = $arg1; $this->arg2 = $arg2; print "__construct is called...\n"; } public function printAttributes() { print '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2."\n"; }}$testObject = new ConstructTest("arg1","arg2"); $testObject->printAttributes();

2. parent: 

     用于在子类中直接调用父类中的方法,功能等同于Java中的super。 

<?php

class BaseClass {
protected $arg1;
protected $arg2;

function __construct($arg1, $arg2) {

$this->arg1 = $arg1;
$this->arg2 = $arg2;
print "__construct is called...\n";
}
function getAttributes() {
return '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2;
}
}

class SubClass extends BaseClass {

protected $arg3;

function __construct($baseArg1, $baseArg2, $subArg3) {

parent::__construct($baseArg1, $baseArg2);
$this->arg3 = $subArg3;
}
function getAttributes() {
return parent::getAttributes().' $arg3 = '.$this->arg3;
}
}
$testObject = new SubClass("arg1","arg2","arg3");
print $testObject->getAttributes()."\n";

3. self:

     在类内调用该类静态成员和静态方法的前缀修饰,对于非静态成员变量和函数则使用this。 

转载于:https://www.cnblogs.com/cgdblog/p/7325559.html

你可能感兴趣的文章
Dynamics CRM 报表导出EXCEL 列合并问题的解决方法
查看>>
MTK6573的LDO控制
查看>>
【工具】-RAP接口管理工具
查看>>
***单文件搞定微信支付和支付宝支付
查看>>
Adminimize 插件:WordPress根据用户角色显示/隐藏某些后台功能
查看>>
运维老鸟教你安装centos6.5如何选择安装包
查看>>
Entity Framework Code-First(13):Configure Many-to-Many
查看>>
Hash的应用
查看>>
如何解决缺少OCX问题,如何在win7 64位下注册OCX
查看>>
《程序员代码面试指南》第五章 字符串问题 将整数字符串转成整数值
查看>>
python模拟登录人人网
查看>>
SSM整合过程中出现的问题
查看>>
37、如何在函数中设置一个全局变量 ?
查看>>
java 类加载机制总结
查看>>
HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)
查看>>
62. Unique Paths不同路径
查看>>
观察者模式
查看>>
容器和算法2 - C++快速入门48(完)
查看>>
Survival Model介绍
查看>>
关于对handle的理解
查看>>