How is the php object-oriented parent:: keyword used?
在PHP中不能定义重名的函数,也包括不能再同一个类中定义重名的方法,所以也就没有方法重载。
前言
最近在做THINKPHP开发项目中,用到了 parent:: 关键字,实际上 parent::关键字 是PHP中常要用到的一个功能,这不仅仅是在 THINKPHP 项目开发中,即使是一个小型的企业站点,也会有许多相同的function功能,我们可以封装成一个父类 class,然后多个子类直接继承公共部分父类。
PHP5中使用parent::来引用父类的方法。
parent:: 可用于调用父类中定义的成员方法。
parent::的追溯不仅于直接父类。
通过parent::调用父类方法
<!-- 声明一个员工类,经理类继承自员工类 --> <? class employee{ protected $sal=3000; public function getSal(){ $this->sal = $this->sal + 1200; return $this->sal ; } } class Manager extends employee { //如果想让经理在员工工资的基础上多发1500元. //必须先调用父类的getSal()方法. public function getSal(){ parent::getSal();// 这里调用了父类的方法. $this->sal = $this->sal + 1500; return $this->sal ; } } $emp = new employee(); echo "普通员工的工资是 " . $emp->getSal(); echo "<br>"; //xiariboke.com $manager = new Manager(); echo "经理的工资是: " . $manager->getSal(); ?>
程序运行结果:
普通员工的工资是 4200
经理的工资是: 5700
父类的private属性
这个东西解释起来十分的不爽。
Private属性是不能被继承的,如果父类有私有的属性。那么父类的方法只为父类的私有属性服务。
下面的例子看起来很奇怪,在子类中重新定义了一个属性$sal,系统却返回了父类的属性。
<?php class employee{ private $sal=3000; //protected $sal=3000; public function getSal(){ return $this->sal; } } class Manager extends employee { protected $sal=5000; public function getParentSal(){ //这里返回的是父类的private属性. return parent::getSal(); } } $manager = new Manager(); echo "PHP ".phpversion()."<br>"; echo $manager->getSal(); echo "<br>"; echo "parent's \$sal ".$manager->getParentSal(); //xiariboke.com ?>
程序运行结果:
PHP 5.2.9
3000
parent's $sal 3000
如果父类中的属性被子类重写了。结果是这样的。注意 第5行的属性定义变成了protected。
<?php class employee{ //private $sal=3000; protected $sal=3000; public function getSal(){ return $this->sal; } } class Manager extends employee { protected $sal=5000; //xiariboke.com public function getParentSal(){ //这里返回的是父类的private属性. return parent::getSal(); } } $manager = new Manager(); echo "PHP ".phpversion()."<br>"; echo $manager->getSal(); echo "<br>"; echo "parent's \$sal ".$manager->getParentSal(); ?>
程序运行结果:
PHP 5.2.9
5000
parent's $sal 5000
子类中重写的方法对当前private有效。
<?php class employee{ private $sal=3000; public function getSal(){ return $this->sal; } } class Manager extends employee { private $sal=5000; //重写过的方法 public function getSal(){ return $this->sal; } public function getParentSal(){ //这里返回的是父类的private属性. return parent::getSal(); } } $manager = new Manager(); echo "PHP ".phpversion()."<br>"; echo $manager->getSal(); echo "<br>"; echo "parent's \$sal ".$manager->getParentSal(); ?>
程序运行结果:
PHP 5.2.9
5000
parent's $sal 3000
打开zend调试状态看看,内存中的情况。注意最下面,有两个$sal 。分别是 3000 和 5000 。
<?php class employee{ private $sal=3000; public function getSal(){ return $this->sal; } } class Manager extends employee { protected $sal=5000; public function getParentSal(){ return $this->sal; } } $manager = new Manager(); echo "PHP ".phpversion()."<br>"; echo $manager->getSal(); ?>
程序运行结果:
PHP 5.2.9
3000
将父类的属性$sal 改成 protected ,子类重写了父类的属性。在内存中只有一个 $sal 。
<?php class employee{ protected $sal=3000; public function getSal(){ return $this->sal; } } class Manager extends employee { protected $sal=5000; public function getParentSal(){ return $this->sal; } } $manager = new Manager(); echo "PHP ".phpversion()."<br>"; echo $manager->getSal(); ?>
程序运行结果:
PHP 5.2.9
5000
如果你学过java,你会觉得这一切都是很难理解的。
在Java中当子类被创建时,父类的属性和方法在内存中都被创建,甚至构造函数也要被调用。
PHP5不是这样,PHP5调用父类用的是parent:: 而不是 parent-> ,这足以说明PHP5不想在内存中让父类也被创建。PHP5想让继承变的比Java更简单。
适应下就好。
这样调用会让PHP5.1.1溢出。新版不知道有没有问题。
<?php class employee{ private $sal=3000; public function getSal(){ return parent::$this->sal; } } class Manager extends employee { protected $sal=5000; public function getSal(){ return parent::$this->getSal(); } } $manager = new Manager(); echo "PHP ".phpversion()."<br>"; echo $manager->getSal(); ?>
第12行改成这样就好了。注意比较。
return parent:: getSal();
这样的代码引起了递归操作,子类调用父类的方法,父类又调用子类方法。
return parent::$this->getSal();
The above is the detailed content of How is the php object-oriented parent:: keyword used?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
