


This article mainly introduces the object-oriented features of the PHP introductory tutorial, and analyzes the inheritance, polymorphism, interface, ## involved in php object-oriented in combination with examples. #Abstract class and abstract methods, etc. Friends who need it can refer to the following
The examples in this article describe the object-oriented characteristics 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;'); //创建一个电脑类 class Computer { //什么叫做类内,就是创建类的花括号内的范围叫做类内,其他地方则类外。 //public 是对字段的公有化,这个字段类外即可访问,赋值和取值 public $_name = '联想'; } $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 = '联想'; } $computer = new Computer(); echo $computer->_name; ?>Demo3.php
<?php header('Content-Type:text/html; charset=utf-8;'); class Computer { private $_name = '联想'; //这个时候我采用一个公共对外的方法来访问私有字段 //因为私有字段只能在类内访问,而对外的公共方法是类内的。 //更而公共方法又是公共的,所以类外又可访问。 public function _run(){ //字段在类内调用的时候必须是类 -> 字段,而$_name只是一个普通变量而已。 //字段在类外调用的方法是对象 -> 字段,而类内就必须使用 Computer -> _name //但是在本类中,可以使用一个关键字来代替字来代替 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; } //必须写一个对内的入口,对私有字段进行赋值 public function setName($name) { //这里的 $name 只是一个变量而已,参数而已 //$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; //当类外的对象直接调用私有字段时,会跟着去检查是否有拦截器, //如果直接对 $_name 进行赋值,那么set 方法就会拦截住,就不会报错了。 //采用拦截器进行赋值和取值 //赋值 private function set($_key,$_value){ //采用$_key = '_name',那么 $_value = '联想'; //$this ->_name = '联想'; $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 = '联想'; $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 方法私有了,还是可以执行,是因为 //因为目前程序的指针已经在类内了。而类内可以执行封装的方法 //类内执行私有方法,不会出现任何错误。 //它只需要间接的拦截就可以了。拦截是在内类执行的。 //说白了,set() 和 get() 是 PHP 内置的方法,具有一定的特殊性 private function set($_key, $_value) { $this->$_key = $_value; } private function get($_key) { return $this->$_key; } } $computer = new Computer (); $computer->_name = '联想'; $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(){ //如果是静态成员字段,那么就应该用 self 来调用,而不是 $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 = '联想'; public function _run(){ echo '联想在运行!'; } } //子类,笔记本电脑类 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 = '联想'; public function _run(){ echo '联想在运行!'; } } 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 = '联想'; protected function _run(){ return '联想在运行!'; } } 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 = '联想'; public function _run(){ return '联想在运行!'; } } class NoteComputer extends Computer { //我子类已经覆盖了父类的字段和方法, //但是我又要调用父类的字段和方法,那怎么办呢? public $_name = 'Dell'; public function _run(){ echo 'Dell在运行!'; echo parent :: _run(); } } $noteComputer = new NoteComputer(); echo $noteComputer -> _name; $noteComputer -> _run(); //DellDell在运行!联想在运行! ?>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;' ); //创建一个抽象类,只要在 class 前面加上 abstract 就是抽象类了 //抽象类不能够被实例化,就是创建对象 //只在类里面有一个抽象方法,那么这个类必须是抽象类,类前面必须加上 abstract abstract class Computer { public $_name = '联想'; //抽象类里创建一个抽象方法 //抽象方法不能够实现方法体的内容 abstract public function _run(); //我在抽象类里能否创建一个普通方法 public function _run2(){ echo '我是父类的普通方法'; } } //类不能够实现多继承,只支持单继承。 //抽象类是给子类用来继承的,实现一种规范和资源的共享 class NoteComputer extends Computer { //抽象类的抽象方法,子类必须重写,不然会报错。 //抽象类里的普通方法不需要重写,子类会直接继承下来 public function _run(){ echo '我是子类的方法'; } } $noteComputer = new NoteComputer(); $noteComputer -> _run(); $noteComputer -> _run2(); echo $noteComputer -> _name; ?>Demo18.php
<?php /* * 到底应该用抽象类还是接口呢 * 如果你要继承多个接口的方法规范,那么就用接口好了。 * 如果你要共享一个方法体内容,那么就用抽象类。 * */ header ( 'Content-Type:text/html; charset=utf-8;' ); //创建一个接口 //接口也不能被实例化 //接口是为了规范实现它的子类,以达到统一的目的。也可以共享数据 interface Computer { //成员字段必须是变量 const NAME = '成员 '; //接口里的所有方法都是抽象方法,不能够写方法体 //并且接口的抽象方法不需要写 abstract public function _run(); public function _run2(); } interface Computer2 { public function _run3(); } //子类继承接口的说法,叫做实现,接口可以多实现 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;' ); //什么叫做多态,字面意思,多种形态 //一个动作由不同的人去执行,而产生不同的效果或者效果,即为多态。 //一个人通过不同的状态去执行同一种动作,形成不同的效果,也可以称作为多态。 //园丁 剪 修理花草 //理发师 剪 理发 //总裁 剪 裁员 //人 笔记本 运行 win7开机了 //人 台式机 运行 xp开机了 //创建一个接口,来规范运行的方法 interface Computer { public function version(); //这个方法表示采用什么电脑 public function work(); //这台电脑是怎么运行的 } //创建一个笔记本的类实现接口 class NoteComputer implements Computer { public function version() { echo '笔记本'; } public function work() { echo '可以便携式运行 win7'; } } //创建一个台式机的类实现接口 class DesktopComputer implements Computer { public function version() { echo '台式机'; } public function work() { echo '在工作站运行 XP'; } } //创建一个用户 class Person { //创建一个方法来接受电脑(笔记本电脑,也可以是台式电脑) //怎么接受,将他们的对象传进来就 OK 啦。 public function _run($type) { echo '这个人的'; $type -> version(); $type ->work(); } } //多态的原理,就是类都写好了,不要去修改它,只要在类外的调用参数的更改 //而最后的结果也会得到更改,那么这个就是多态。 //有一个接口,两个类,一个是笔记本的类,一个是台式机的类 //创建了笔记本 $noteComputer = new NoteComputer(); //创建台式机 $desktopComputer = new DesktopComputer(); //创建一个人 $person = new Person(); //使用电脑 $person -> _run($noteComputer); //这种传递,叫做对象引用的传递 ?>
The above is the detailed content of Inheritance, polymorphism, interface, abstract class, abstract method example tutorial in PHP object-oriented. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),