Home Backend Development PHP Tutorial PHP object-oriented learning parent::keyword

PHP object-oriented learning parent::keyword

Jan 21, 2017 pm 01:20 PM

前言

最近在做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();
?>
Copy after login


程序运行结果:

普通员工的工资是 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&#39;s \$sal ".$manager->getParentSal();
//xiariboke.com
?>
Copy after login

程序运行结果:

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&#39;s \$sal ".$manager->getParentSal();
  
?>
Copy after login

程序运行结果:

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&#39;s \$sal ".$manager->getParentSal();
  
?>
Copy after login

程序运行结果:

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();
  
?>
Copy after login

程序运行结果:

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();
  
?>
Copy after login

程序运行结果:

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();
  
?>
Copy after login

第12行改成这样就好了。注意比较。

return parent:: getSal();

这样的代码引起了递归操作,子类调用父类的方法,父类又调用子类方法。

return parent::$this->getSal();

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

更多PHP面向对象学习之parent::关键字相关文章请关注PHP中文网!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles