


Detailed explanation of classes and objects (inheritance) in php_php examples
在php中,类型的继承使用extends关键字,而且最多只能继承一个父类,php不支持多继承。这篇文章主要介绍了php中的类与对象(继承),需要的朋友可以参考下
简介
在php中,类型的继承使用extends关键字,而且最多只能继承一个父类,php不支持多继承。
class MyClass { public $dat = 0; public function construct($dat) { $this->dat = $dat; } public function getDat() { return "$this->dat\n"; } } class MySubClass extends MyClass { public function getDat() { return "dat: $this->dat\n"; } } $a = new MyClass(3); $b = new MySubClass(4); echo $a->getDat(); // 3 echo $b->getDat(); // dat: 4
方法覆盖
包括构造函数在内,子类可以重新定义同名的类方法以覆盖父类方法。覆盖时遵循以下规则:
1.除构造函数之外,其他函数在覆盖时,函数的参数列表必须相同
2.包括构造函数在内,方法被覆盖后,调用子类方法时并不会自动调用父类方法
3.如果父类要禁止方法被子类覆盖,可以使用final来声明方法,这时如果子类仍要覆盖父类方法,将会出错
class MyClass { private $name = ""; public $num = 0; public $str = ""; public function construct($name) { $this->name = $name; $this->num = 100; $this->str = "none"; } public function getName() { return $this->name; } } class MySubClass extends MyClass { public function construct($name, $str) { parent::construct($name); // 调用父类方法 $this->num = "0"; $this->str = $str; echo parent::getName()."\n"; // 调用父类方法 } public function getName() { return parent::getName()."$this->str\n"; // 调用父类方法 } } $b = new MySubClass("myName", true); // myName echo $b->getName(); // myName1 class MyClass { final public function getName() { } }
属性重定义
在子类中,可以访问父类中的public和protected属性成员,除非重定义了同名的自有属性,这时,父类中的属性将无法访问。
方法则不同,子类对方法进行覆盖后,仍然可以访问到父类方法。
class MyClass { public $a = 1; protected $b = 2; private $c = 3; public function f1() { echo "MyClass f1\n"; echo "\$a:$this->a; \$b:$this->b; \$c:$this->c;\n"; } protected function f2() { echo "MyClass f2\n"; echo "\$a:$this->a; \$b:$this->b; \$c:$this->c;\n"; } private function f3() { echo "MyClass f3\n"; } } class MySubClass extends MyClass { public $b = 22; public $c = 33; public function f1() { echo "MySubClass f1\n"; // 继承到父类中的$a属性,直接使用 echo "\$a:$this->a; \$b:$this->b; \$c:$this->c;\n"; // 调用父类中的同名方法 parent::f1(); // 继承到父类中的f2()方法,直接使用 $this->f2(); } // 父类的f3()是私有的,这里的定义与父类无关 public function f3() { echo "MySubClass f3\n"; } } $b = new MySubClass; $b->f1();echo "\n"; /* MySubClass f1 $a:1; $b:22; $c:33; MyClass f1 $a:1; $b:22; $c:3; MyClass f2 $a:1; $b:22; $c:3; */ $b->f3();echo "\n"; /* MySubClass f3 */
重定义父类(同名)属性时,属性的可访问性可以变得更开放,但不能更严格,也就是说,父类中的public属性,不能在子类中修改为private属性。
如果通过子类对象调用父类方法,那么该父类方法在访问属性时,对于重定义了的同名属性,public和protected的属性将访问到子类版本,private属性将访问到父类版本。也可以理解为,public和protected属性可以被重定义(父类的版本被重定义,从而不存在了),而private并未被重定义(父类中的属性仍然存在,通过父类方法进行访问,与子类中是否有同名属性毫不相干)。
class MyClass { public $a = 1; protected $b = 2; private $c = 3; public function f1() { echo "\$a:$this->a; \$b:$this->b; \$c:$this->c;\n"; } } class MySubClass extends MyClass { public $a = 11; // 必须为public protected $b = 22; // 必须为protected或public private $c = 33; public function f2() { echo "\$a:$this->a; \$b:$this->b; \$c:$this->c;\n"; } } $b = new MySubClass; $b->f1(); // $a:11; $b:22; $c:3; $b->f2(); // $a:11; $b:22; $c:33;
范围解析操作符 ::
又冒号常用于访问类常量、类静态变量,也用于在方法覆盖时调用父类版本。与其搭配的还包括parent、self、static等关键字。
class MyClass { const Name0 = "MyClass"; // 类常量 public static $id0 = 0; // 类变量 public function put() { // 将被子类覆盖的方法 echo "MyClass put()\n"; } } class MySubClass extends MyClass { const Name1 = "MySubClass"; public static $id1 = 1; public function put() { parent::put(); // 调用父类版本的对象方法 echo parent::Name0 . "\n"; // 父类常量 echo parent::$id0 . "\n"; // 父类变量 echo self::Name1."\n"; // 子类常量 echo self::$id1 . "\n"; // 子类变量 echo static::Name1 . "\n"; // 子类常理 echo static::$id1 . "\n"; // 子类变量 } } $a = "MyClass"; $ca = new MyClass; $cb = new MySubClass; $cb->put(); echo MyClass::Name0 . "\n"; echo MyClass::$id0 . "\n"; echo $a::Name0 . "\n"; echo $a::$id0 . "\n"; echo $ca::Name0 . "\n"; echo $ca::$id0 . "\n";
在子类中访问父类中的成员时,应避免直接使用父类类名,而应使用parent::,以免破坏父类的封装性。
final
声明为final的方法不能被子类覆盖,如果类声明为final,则此类不能被继承。
// 声明为final的类不能被继承 final class MyClass { private $dat; public function construct($dat) { $this->dat = $dat; } // final方法不能被覆盖,不过此类已经是final类,方法无必要在声明为final了 final public function getDat() { return $this->dat; } }
总结
以上所述是小编给大家介绍的php中的类与对象(继承),希望对大家有所帮助!!
相关推荐:
The above is the detailed content of Detailed explanation of classes and objects (inheritance) in php_php examples. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software