1. Definition
<code>class 类名 { <span>...</span> }</code>
2. Load class
<code><span>require</span><span>'./People.class.php'</span>;</code>
3. Automatically load class
In order to use a class, it is troublesome to include the class definition every time. Starting from PHP5, a __autoload() function is defined to implement automatic loading of classes. PHP will automatically call this function when using an undefined class.
<code><span><span>function</span><span>__autoload</span><span>(<span>$classname</span>)</span> {</span><span>require_once</span><span>'./'</span> . <span>$classname</span> . <span>'.php'</span>; }</code>
4. Class methods
<code>[ <span>static</span> | <span>final</span> ] 访问控制修饰符 <span><span>function</span> 方法名<span>(参数)</span> {</span> ... } 关键字 <span>static</span><span>final</span> 为可选项, 访问控制修饰符为<span>public</span>,<span>protected</span>,<span>private</span>中的一个。如果不指定,默认为<span>public</span>。 <span>static</span>关键字修饰的类方法为静态方法。在静态方法中,只能调用静态变量,不能调用普遍变量。 在普通方法中,可以调用静态变量。 在类的内部调用静态方法:<span>self</span>::静态方法。 在类的内部调用父类的静态方法:<span>parent</span>::静态方法。 如果在外部调用静态方法,则不用实例化,直接调用。 类名::静态方法</code>
5. Class attributes
<code>访问控制修饰符 [<span>static</span>] 属性名称; 访问控制修饰符为<span>public</span>,<span>protected</span>,<span>private</span>中的一个。默认为<span>public</span>。 如果要在类的方法中,访问属性:<span>$this</span>->属性名; 在类的内部访问静态属性:<span>self</span>::静态属性。 访问父类的静态属性:<span>parent</span>::静态属性。 注意,这里的静态属性名是要加 $ 美元符号的。 <span>self</span>::<span>$dollars</span> = <span>$dollars</span>; 在类外访问静态属性: 类名::属性名; 前提为可以访问,也是有 $ 符号。</code>
6. Class constants
<code>const <span>MALE</span> = <span>'男'</span>; <span>//</span>常量名前面不加 <span>$ </span>。 类内访问: <span>self:</span><span>:</span>常量名 <span>parent:</span><span>:</span>常量名 不加 <span>$ </span> , <span>self:</span><span>:MALE</span>类外访问: 类名<span>:</span><span>:</span>常量名 ; 不加 <span>$ </span>。</code>
7. Constructors and destructors
<code><span>function</span> __construct() { <span>...</span> // 自动调用 } <span>function</span> __destruct() { <span>...</span> // 无参 }</code>
8. Class inheritance
<code>class 子类名 extends 父类名 { <span>...</span> } 如果子类中定义了构造方法,则子类在实例化时不会自动调用父类的构造方法。对于析构方法也是如此, 如果子类中定义了析构方法,则子类的实例在被销毁时不会自 动调用父类的析构方法。 显式调用父类的构造方法: <span>function</span> __construct() { <span>...</span> parent::__construct() { <span>...</span> } <span>...</span> } 显式调用父类的析构方法: <span>function</span> __destruct() { <span>...</span> parent::__destruct() { <span>...</span> } <span>...</span> }</code>
9. Polymorphism
Polymorphism is divided into static polymorphism and dynamic polymorphism
<code>静态多态:一个同名函数或者一个类中的同名方法,根据参数列表(类型以及个数)的不同来区别语义, 即所谓的函数重载。但PHP不支持函数重载。 动态多态:类的成员方法,能根据调用它的对象的类型的不同,自动做出适应性调整, 而且调整是发生在程序运行时的。PHP 中通过抽象类和接口 技术来实现动态多态性。</code>
Subclass overrides the parent class:
As long as a method with the same name as the parent class is defined in the subclass, it can be overridden. To access the parent class, use parent::method with the same name as the parent class.
If the parent class does not want its methods to be overridden by the subclass, use final to modify the parent class’s methods.
10. Abstract classes and abstract methods
An abstract class is a class that cannot be instantiated and can only be inherited by other classes as a parent class. An abstract class must contain an abstract method.
The so-called abstract method is a method without specific implementation, and its corresponding function body is empty. The details of abstract methods can only be implemented in subclasses, and subclasses must implement all abstract methods in the abstract class they inherit.
<code>abstract class 抽象类名 { <span>...</span> abstract [public | protected ] <span>function</span> 抽象方法名(参数) { <span>...</span> } } 其中,抽象方法的访问控制符只能为public,protected之一。如果抽象方法声明为public,则 子类中实现的方法也应声明为public;如果抽象方法声明为protected, 则子类中实现的方法既可以为protected,也可以声明为public。</code>
11. Interface
Whether it is a universal class or an abstract class, it can only implement single inheritance, that is, a subclass can only inherit one parent class. In fact, PHP only supports single inheritance. If you want to implement multiple inheritance, use interface technology to achieve it.
The difference between interfaces and classes:
<code>可以认为接口也是一种类,只是这种类中可以定义常量,但不能定义属性变量;可以定义方法, 但方法必须为空。也就是说,接口中只能定义常量和尚 未实现的方法,且方法必须为<span>public</span>(可以省略, 因为类的方法默认就是<span>public</span>)。 <span><span>interface</span> 接口名 {</span><span>const</span> 常量名 = 值; <span><span>function</span> 方法名<span>(参数)</span> {</span> ... } } 实现: <span><span>class</span> 子类名 <span>implements</span> 接口名1,接口名2 .. {</span> ... }</code>
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above has introduced the 34 PHP classes, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果,其目的是封装一段可重复使用的代码,提高代码的可重用性和可维护性。

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

每年Apple发布新的iOS和macOS大版本之前,用户都可以提前几个月下载测试版抢先体验一番。由于公众和开发人员都使用该软件,所以苹果公司为两者推出了developer和public版即开发者测试版的公共测试版。iOS的developer版和public版有什么区别呢?从字面上的意思来说,developer版是开发者测试版,public版是公共测试版。developer版和public版面向的对象不同。developer版是苹果公司给开发者测试使用的,需要苹果开发者帐号才可以收到下载并升级,是

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

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

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

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


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

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
