


PHP introductory tutorial: Analysis of object-oriented features (inheritance, polymorphism, interfaces, abstract classes, abstract methods, etc.)
Dec 22, 2016 am 11:52 AMThe examples in this article describe the object-oriented features of PHP. Share it with everyone for your reference, the details are as follows:
Demo1.php
<?php header('Content-Type:text/html; charset=utf-8;'); //創(chuàng)建一個電腦類 class Computer { //什么叫做類內(nèi),就是創(chuàng)建類的花括號內(nèi)的范圍叫做類內(nèi),其他地方則類外。 //public 是對字段的公有化,這個字段類外即可訪問,賦值和取值 public $_name = '聯(lián)想'; } $computer = new Computer(); $computer -> _name = 'Dell'; echo $computer->_name; ?>
Demo2.php
<?php header('Content-Type:text/html; charset=utf-8;'); class Computer { //private 是私有化,即對字段進行封裝的操作,類外無法訪問,取值和賦值都不能操作 private $_name = '聯(lián)想'; } $computer = new Computer(); echo $computer->_name; ?>
Demo3.php
<?php header('Content-Type:text/html; charset=utf-8;'); class Computer { private $_name = '聯(lián)想'; //這個時候我采用一個公共對外的方法來訪問私有字段 //因為私有字段只能在類內(nèi)訪問,而對外的公共方法是類內(nèi)的。 //更而公共方法又是公共的,所以類外又可訪問。 public function _run(){ //字段在類內(nèi)調(diào)用的時候必須是類 -> 字段,而$_name只是一個普通變量而已。 //字段在類外調(diào)用的方法是對象 -> 字段,而類內(nèi)就必須使用 Computer -> _name //但是在本類中,可以使用一個關(guān)鍵字來代替字來代替 Computer ,那就是 $this echo $this ->_name; } } $computer = new Computer(); $computer -> _run(); ?>
Demo4.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { private $name; private $model; private $cpu; private $keyboard; private $show; private $zb; //必須寫個對外的入口,才可以取到 public function getName() { return $this->name; } //必須寫一個對內(nèi)的入口,對私有字段進行賦值 public function setName($name) { //這里的 $name 只是一個變量而已,參數(shù)而已 //$this->name 才是類的字段 $this->name = $name; } } $computer = new Computer (); echo $computer->getName(); $computer->setName('Dell'); echo $computer->getName(); ?>
Demo5.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { private $_name; private $_model; private $_cpu; //當類外的對象直接調(diào)用私有字段時,會跟著去檢查是否有攔截器, //如果直接對 $_name 進行賦值,那么__set 方法就會攔截住,就不會報錯了。 //采用攔截器進行賦值和取值 //賦值 private function __set($_key,$_value){ //采用$_key = '_name',那么 $_value = '聯(lián)想'; //$this ->_name = '聯(lián)想'; $this ->$_key = $_value; } //取值 private function __get($_key){ return $this -> $_key; //如果 $_key = '_name' 那么 $this -> _name; //如果 $_key = '_cpu' 那么 $this -> _cpu; //如果 $_key = '_model' 那么 $this -> _model; } } $computer = new Computer (); $computer->_name = '聯(lián)想'; $computer->_cpu = '四核'; $computer->_model = 'i7'; echo $computer->_name; echo $computer->_cpu; echo $computer->_model; ?>
Demo6.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { private $_name; private $_model; private $_cpu; //__set 和 __get 方法私有了,還是可以執(zhí)行,是因為 //因為目前程序的指針已經(jīng)在類內(nèi)了。而類內(nèi)可以執(zhí)行封裝的方法 //類內(nèi)執(zhí)行私有方法,不會出現(xiàn)任何錯誤。 //它只需要間接的攔截就可以了。攔截是在內(nèi)類執(zhí)行的。 //說白了,__set() 和 __get() 是 PHP 內(nèi)置的方法,具有一定的特殊性 private function __set($_key, $_value) { $this->$_key = $_value; } private function __get($_key) { return $this->$_key; } } $computer = new Computer (); $computer->_name = '聯(lián)想'; $computer->_cpu = '四核'; $computer->_model = 'i7'; echo $computer->_name; echo $computer->_cpu; echo $computer->_model; ?>
Demo7.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { const NAME = 'DELL'; } //常量的輸出方法 類::常量 echo Computer::NAME; //DELL ?>
Demo8.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { public $_count = 0; public function _add(){ $this -> _count++; //$_count = $_count+1 $_count++ } } //做一個累計的效果 $computer1 = new Computer(); $computer1 ->_add(); $computer1 ->_add(); $computer1 ->_add(); echo $computer1 -> _count; echo '<br />'; $computer2 = new Computer(); $computer2 ->_add(); $computer2 ->_add(); $computer2 ->_add(); echo $computer2 -> _count; ?>
Demo9.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { public static $_count = 0; public function _add(){ //如果是靜態(tài)成員字段,那么就應該用 self 來調(diào)用,而不是 $this self::$_count++; } } //做一個累計的效果 $computer1 = new Computer(); $computer1 ->_add(); echo Computer::$_count; $computer1 ->_add(); echo Computer::$_count; $computer1 ->_add(); echo Computer::$_count; echo '<br />'; $computer2 = new Computer(); $computer2 ->_add(); echo Computer::$_count; $computer2 ->_add(); echo Computer::$_count; $computer2 ->_add(); echo Computer::$_count; ?>
Demo10.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { public static $_count = 0; public static function _add(){ self::$_count++; } } Computer::_add(); Computer::_add(); Computer::_add(); echo Computer::$_count; ?>
Demo11.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { } $computer = new Computer(); echo $computer instanceof Computer; ?>
Demo12.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); //這是父類,電腦類 class Computer { public $_name = '聯(lián)想'; public function _run(){ echo '聯(lián)想在運行!'; } } //子類,筆記本電腦類 class NoteComputer extends Computer { } $noteComputer = new NoteComputer(); echo $noteComputer -> _name; $noteComputer -> _run(); ?>
Demo13.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { public $_name = '聯(lián)想'; public function _run(){ echo '聯(lián)想在運行!'; } } class NoteComputer extends Computer { //我不需要父類的字段和方法,那么可以采用重寫的方法覆蓋掉父類的字段和方法 public $_name = 'Dell'; public function _run(){ echo 'Dell在運行!'; } } $noteComputer = new NoteComputer(); echo $noteComputer -> _name; $noteComputer -> _run(); ?>
Demo14.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { //私有化,但是無法被子類繼承,這個時候就應該用受保護的修飾符來封裝 protected $_name = '聯(lián)想'; protected function _run(){ return '聯(lián)想在運行!'; } } class NoteComputer extends Computer { public function getTop() { echo $this->_name; echo $this->_run(); } } $noteComputer = new NoteComputer(); $noteComputer -> getTop(); ?>
Demo15.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { public $_name = '聯(lián)想'; public function _run(){ return '聯(lián)想在運行!'; } } class NoteComputer extends Computer { //我子類已經(jīng)覆蓋了父類的字段和方法, //但是我又要調(diào)用父類的字段和方法,那怎么辦呢? public $_name = 'Dell'; public function _run(){ echo 'Dell在運行!'; echo parent :: _run(); } } $noteComputer = new NoteComputer(); echo $noteComputer -> _name; $noteComputer -> _run(); //DellDell在運行!聯(lián)想在運行! ?>
Demo16.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); //final 如果加在類前面,表示這個類不能被繼承 // final class Computer { // } class Computer { //final 如果加在方法前面,表示不能夠重寫些方法 final public function _run(){ } } class NoteComputer extends Computer { public function _run(){ } } $noteComputer = new NoteComputer(); ?>
Demo17.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); //創(chuàng)建一個抽象類,只要在 class 前面加上 abstract 就是抽象類了 //抽象類不能夠被實例化,就是創(chuàng)建對象 //只在類里面有一個抽象方法,那么這個類必須是抽象類,類前面必須加上 abstract abstract class Computer { public $_name = '聯(lián)想'; //抽象類里創(chuàng)建一個抽象方法 //抽象方法不能夠?qū)崿F(xiàn)方法體的內(nèi)容 abstract public function _run(); //我在抽象類里能否創(chuàng)建一個普通方法 public function _run2(){ echo '我是父類的普通方法'; } } //類不能夠?qū)崿F(xiàn)多繼承,只支持單繼承。 //抽象類是給子類用來繼承的,實現(xiàn)一種規(guī)范和資源的共享 class NoteComputer extends Computer { //抽象類的抽象方法,子類必須重寫,不然會報錯。 //抽象類里的普通方法不需要重寫,子類會直接繼承下來 public function _run(){ echo '我是子類的方法'; } } $noteComputer = new NoteComputer(); $noteComputer -> _run(); $noteComputer -> _run2(); echo $noteComputer -> _name; ?>
Demo18.php
<?php /* * 到底應該用抽象類還是接口呢 * 如果你要繼承多個接口的方法規(guī)范,那么就用接口好了。 * 如果你要共享一個方法體內(nèi)容,那么就用抽象類。 * */ header ( 'Content-Type:text/html; charset=utf-8;' ); //創(chuàng)建一個接口 //接口也不能被實例化 //接口是為了規(guī)范實現(xiàn)它的子類,以達到統(tǒng)一的目的。也可以共享數(shù)據(jù) interface Computer { //成員字段必須是變量 const NAME = '成員 '; //接口里的所有方法都是抽象方法,不能夠?qū)懛椒w //并且接口的抽象方法不需要寫 abstract public function _run(); public function _run2(); } interface Computer2 { public function _run3(); } //子類繼承接口的說法,叫做實現(xiàn),接口可以多實現(xiàn) class NoteComputer implements Computer,Computer2 { public function _run() { echo '我重寫了run'; } public function _run3() { echo '我重寫了run3'; } public function _run2() { echo '我重寫了run2'; } } $noteComputer = new NoteComputer(); $noteComputer -> _run(); $noteComputer -> _run2(); $noteComputer -> _run3(); echo NoteComputer::NAME; //接口 :: 常量 //echo Computer::NAME; ?>
Demo19.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); //什么叫做多態(tài),字面意思,多種形態(tài) //一個動作由不同的人去執(zhí)行,而產(chǎn)生不同的效果或者效果,即為多態(tài)。 //一個人通過不同的狀態(tài)去執(zhí)行同一種動作,形成不同的效果,也可以稱作為多態(tài)。 //園丁 剪 修理花草 //理發(fā)師 剪 理發(fā) //總裁 剪 裁員 //人 筆記本 運行 win7開機了 //人 臺式機 運行 xp開機了 //創(chuàng)建一個接口,來規(guī)范運行的方法 interface Computer { public function version(); //這個方法表示采用什么電腦 public function work(); //這臺電腦是怎么運行的 } //創(chuàng)建一個筆記本的類實現(xiàn)接口 class NoteComputer implements Computer { public function version() { echo '筆記本'; } public function work() { echo '可以便攜式運行 win7'; } } //創(chuàng)建一個臺式機的類實現(xiàn)接口 class DesktopComputer implements Computer { public function version() { echo '臺式機'; } public function work() { echo '在工作站運行 XP'; } } //創(chuàng)建一個用戶 class Person { //創(chuàng)建一個方法來接受電腦(筆記本電腦,也可以是臺式電腦) //怎么接受,將他們的對象傳進來就 OK 啦。 public function _run($type) { echo '這個人的'; $type -> version(); $type ->work(); } } //多態(tài)的原理,就是類都寫好了,不要去修改它,只要在類外的調(diào)用參數(shù)的更改 //而最后的結(jié)果也會得到更改,那么這個就是多態(tài)。 //有一個接口,兩個類,一個是筆記本的類,一個是臺式機的類 //創(chuàng)建了筆記本 $noteComputer = new NoteComputer(); //創(chuàng)建臺式機 $desktopComputer = new DesktopComputer(); //創(chuàng)建一個人 $person = new Person(); //使用電腦 $person -> _run($noteComputer); //這種傳遞,叫做對象引用的傳遞 ?>
I hope this article will be helpful to everyone in PHP programming.
For more PHP introductory tutorials on object-oriented feature analysis (inheritance, polymorphism, interfaces, abstract classes, abstract methods, etc.), please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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)

How to use Go language to implement object-oriented event-driven programming Introduction: The object-oriented programming paradigm is widely used in software development, and event-driven programming is a common programming model that realizes the program flow through the triggering and processing of events. control. This article will introduce how to implement object-oriented event-driven programming using Go language and provide code examples. 1. The concept of event-driven programming Event-driven programming is a programming model based on events and messages, which transfers the flow control of the program to the triggering and processing of events. in event driven

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming In object-oriented programming, design pattern is a commonly used software design method, which can improve the readability, maintainability and scalability of the code. Flyweight pattern is one of the design patterns that reduces memory overhead by sharing objects. This article will explore how to use flyweight mode in PHP to improve program performance. What is flyweight mode? Flyweight pattern is a structural design pattern whose purpose is to share the same object between different objects.

By mastering tracking object status, setting breakpoints, tracking exceptions and utilizing the xdebug extension, you can effectively debug PHP object-oriented programming code. 1. Track object status: Use var_dump() and print_r() to view object attributes and method values. 2. Set a breakpoint: Set a breakpoint in the development environment, and the debugger will pause when execution reaches the breakpoint, making it easier to check the object status. 3. Trace exceptions: Use try-catch blocks and getTraceAsString() to get the stack trace and message when the exception occurs. 4. Use the debugger: The xdebug_var_dump() function can inspect the contents of variables during code execution.
