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.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


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

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.

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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),

WebStorm Mac version
Useful JavaScript development tools
