OOP in PHP

Source: Internet
Author: User

One , in OOP, there is a pattern called a singleton pattern, that What is a singleton mode?

PHP Singleton mode is that an object is generated only once, but the object can be used by many other objects.

     In its core structure, it contains only a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources.

The most common scenario for a singleton pattern is the database connection operation. We know that the operation of generating an object is implemented with the new function, but the new object consumes memory, and sometimes the same object may be generated multiple times in different files, which creates a waste of system resources. However, using singleton mode can be a good way to avoid this situation.

The sample code is as follows:

There are several requirements for using singleton mode:

1. The constructor needs to be marked private (access control: Prevent external code from creating objects using the new operator), the Singleton class cannot be instantiated in other classes, and can only be instantiated by itself;

2. Have a static member variable that holds an instance of the class;

3. Have a public static method that accesses this instance (common getinstance () method to instantiate a singleton class, through the instanceof operator can detect whether the class has been instantiated);

4. If you are rigorous, you also need to create a __clone () method to prevent objects from being copied (cloned). (I did not create above)

Using the singleton pattern benefits, summarize:

1, the application of PHP is mainly in the database application, so there will be a large number of database operations in an application, using a singleton mode, you can avoid a large number of new operations to consume resources.

2, if the system needs to have a class to control some of the configuration information globally, then using the singleton mode can be easily implemented. This can be see the frontcontroller part of ZF.

3, in a page request, easy to debug.

second, in the Phpoop there is another way to call the magic side 1, in PHP, all the class methods that begin with __ (two underscore) are reserved as magic methods, so in addition to the above magic method, it is not recommended to prefix __ when defining class methods.


The introduction of Magic method is the further realization of object-oriented programming thought, and overloading is realized by magic method.
2. The main methods of PHP magic are:
 The most common are __construct (), __destruct (), __get (), __set (), etc.:

__construct and __destruct are constructors and destructors for classes.


3. Summary of Magic Methods

1, __construct (): constructor, new object is called automatically
2, __destruct (): destructor, automatically called when an object is destroyed
3. __get (): When accessing a private property in a class, it is called automatically, passing the Read property name, returning the $this-> property name
4, __isset (): When using the Isset function to detect the object private property, automatic invocation, passing the detected property name, return Isset ($this property name)
5. __set (): Automatic invocation of all types of private property assignments, passing property names and property values that need to be set
6. __unset (): Automatically called when the object private property is deleted using unset (), passing the deleted property name, performing the unset ($this-property name) in the method
7. __tostring (): Automatically called when using Echo to print an object. Returns the content that you want to display when you print the object, and the return must be a string;
8, __call (): called automatically when a method that is undefined or not exposed in a class is invoked. Pass the called function name, and an array of parameter lists;
9, __clone (): When cloning an object using the Clone keyword, it is called automatically. The function is to initialize the newly cloned object to be assigned value;
10, __sleep (): Automatically called when the object is serialized. Returns an array in which the values in the array are the properties that can be serialized;
11, __wakeup (): Automatically called when the object is deserialized. To deserialize the newly generated object, initialize the assigned value;
12, __autoload (): You need to declare a function outside the class. Called automatically when an undeclared class is instantiated. Passing the instantiated class name, you can automatically load the corresponding class file using the class name.

OOP in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.