


PHP object-oriented basics (interface, class), php-oriented_PHP tutorial
PHP object-oriented basics (interfaces, classes), PHP is oriented
Introducing the basic knowledge of PHP object-oriented
1. Definition of interface interface, class definition class, class supports abstract and final modifiers, abstract is modified into abstract class, abstract class
Does not support direct instantiation, and final-modified classes/methods cannot be inherited/method overridden.
2. The interface is implemented through implements, and class inheritance extends
<span>interface</span><span> IShape{ </span><span>function</span><span> draw_core(); }; </span><span>class</span> PathShape <span>implements</span><span> IShape{ </span><span>public</span> <span>function</span><span> draw_core(){} } </span><span>class</span> Rectangle <span>extends</span><span> PathShape{ </span><span>public</span> <span>function</span><span> draw_core(){ </span><span>//</span><span>overide draw_core</span> <span> } }</span>
3. Static variables and constants (static, const)
a. There is no need to add the dollar modifier $ in front of the constant declaration variable name, and static variables require
b. Both are accessed through classes, static variable methods Sometimes you need to add the $ dollar modifier before the variable name
<span>class</span><span> MyClass{ </span><span>const</span><span> M_CONST_VALUE; </span><span>static</span> <span>$M_STATIC_VALUE</span><span>; } MyClass</span>::<span>M_CONST_VALUE ; MyClass</span>::<span>$M_STATIC_VALUE</span>;
c. Access permission modifiers are not supported when declaring constants. Public cannot be added before const. Constants default to public.
<span>const</span> M_CONST ; <span>//</span><span>OK</span> <span>public</span> <span>const</span> M_CONST ; <span>//</span><span> throw exception</span>
4. Access non-static/constant variables and methods within a class through $this, access the parent class through parent, and access static variables and methods within a class through
self. Self essentially points to the class or through static Visit
parent::method(); <span>//</span><span>父类方法</span> <span>$this</span>->method() ; <span>//</span><span>方法实例方法</span> self::<span>$static_value</span> ;<span>//</span><span>访问静态变量</span> <span>static</span>::<span>$static_value</span>;<span>//</span><span>同上</span>
5. The difference between static and self is that self refers to the parsing context, which is also the current class. Static refers to the class that is called
rather than the containing class. A typical example is a singleton
<span>abstract</span> <span>class</span><span> ParentClass{ </span><span>public</span> <span>static</span> <span>function</span><span> createInstance(){ </span><span>return</span> <span>new</span> <span>static</span><span>(); </span><span>//</span><span>这里不能使用self,因为self本意其实指向parentclass的 //如果你使用了self,那么将抛出异常,提示抽象类无法实例化 //而static并不直接指向parentclass而是作用与包含类 //</span> <span> } } </span><span>class</span> ChildClass <span>extends</span><span> ParentClass{ </span><span>// </span> }
7. Use interceptors in classes. PHP interceptors include __get, __set, __inset, __unset, __call. Here we only focus on geth and set interceptors
__get(<span>$property</span><span>) 当访问未定义的属性时候该方法被调用 __set(</span><span>$property</span>,<span>$value</span><span>)当给未定义的属性赋值时被调用 </span><span>class</span><span> MyClass{ </span><span>public</span> <span>function</span> __get(<span>$property</span><span>){ </span><span>echo</span> "Access __get"<span>; </span><span>if</span>(property_exists(<span>$this</span>,<span>$property</span><span>)){ </span><span>return</span> <span>$this</span>-><span>$property</span><span>; }</span><span>else</span><span>{ </span><span>return</span> "unknown"<span>; } } </span><span>public</span> <span>function</span> __set(<span>$property</span>,<span>$value</span><span>){ </span><span>if</span>(!property_exists(<span>$this</span>,<span>$property</span><span>)){ </span><span>$this</span>->Name = <span>$value</span>; <span>//</span><span>变量不存在就直接给$Name赋值</span> <span> } } </span><span>public</span> <span>$Name</span> = "visonme"<span>; }; </span><span>//</span><span>访问</span> <span>$obj</span> = <span>new</span><span> MyClass(); </span><span>$obj</span>->Name ; <span>//</span><span>直接访问变量$Name</span> <span>$obj</span>->Password;<span>//</span><span>Password未定义,先访问__get最后输出unknown //-for __set</span> <span>$obj</span>->password = 'fz-visonme';<span>//</span><span>password不存在,那么将走__setz最后给$Name赋值</span> <span>echo</span> <span>$obj</span>->Name ; <span>//</span><span> output: fz-visonme</span>
8. Class constructor and destructor: __construct, __destruct. The constructor is called when instantiating an object and is mostly used for member variable initialization. Destructor is called when the class is destroyed and is mostly used for finishing work
<span>class</span><span> MyClass{ </span><span>function</span><span> __construct(){} </span><span>function</span><span> __destruct(){} }</span>
9. The object is copied through clone. The clone keyword uses the "value copy" method to generate a new object. The object copy itself is still copied by reference.
a. Simple type assignment
<span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; }; </span><span>$a</span> = <span>new</span><span> MyClass; </span><span>$a</span>->ID = 199<span>; </span><span>$b</span> = <span>clone</span> <span>$a</span><span>; </span><span>echo</span> <span>$b</span>->ID; <span>//</span><span> output: 199</span>
b. Copy of containing objects
<span>class</span><span> Account{ </span><span>public</span> <span>$RMB</span><span>; }; </span><span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; </span><span>public</span> <span>$AccountObj</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$c</span><span>){ </span><span>$this</span>->AccountObj = <span>$c</span><span>; } }; </span><span>$a</span> = <span>new</span> MyClass(<span>new</span><span> Account()); </span><span>$a</span>->AccountObj->RMB= 199<span>; </span><span>$b</span> = <span>clone</span> <span>$a</span><span>; </span><span>echo</span> <span>$b</span>->AccountObj->RMB; <span>//</span><span>output: 199</span> <span>$a</span>->AccountObj->RMB = 100<span>; </span><span>echo</span> <span>$b</span>->AccountObj->RMB; <span>//</span><span>output: 100</span> <span> 在clone后,</span><span>$a的AccountObj改变时候</span>,同时会影响到<span>$b</span>
This result is obviously not what we expect. What we expect is that ab is two independent objects without any correlation.
In order to solve this problem, we can implement __clone inside the class. When we call clone outside, the __clonef method of the class will be called internally, so we can achieve control of clone by overriding __clone. For example Modification of example b
<span>class</span><span> MyClass{ </span><span>public</span> <span>$ID</span><span>; </span><span>public</span> <span>$AccountObj</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$c</span><span>){ </span><span>$this</span>->AccountObj = <span>$c</span><span>; } </span><span>//</span><span>__clone实现clone的控制 //这里内部同时对Account实现一次clone,这里就可以避免b例子中出现的问题</span> <span>public</span> <span>function</span><span> __clone(){ </span><span>$this</span>->ID = 0 ; <span>//</span><span>将ID置为0,如果你需要的话</span> <span>$this</span>->AccountObj = <span>clone</span> <span>$this</span>-><span>AccountObj; } };</span>
We need to know about the __clone method. This method is called on the cloned object, not on the original object. For example, in the example b above
$b = clone $a; //Execution process: Basic copy object $a ---> $b executes __clone()

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.

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

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

Atom editor mac version download
The most popular open source editor
