PHPCookbook Reading Notes & ndash; chapter 2 class and Object

Source: Internet
Author: User
Although php had support for Object-Oriented Programming before PHP5, it was not ideal for processing objects and classes until PHP5 used ZendEngine2 (ZE2) so that PHP can contain advanced object-oriented features. This chapter introduces the object-oriented features of PHP5. At present, many companies propose this part when recruiting PHPer.

Although php had support for Object-Oriented Programming before PHP5, it was not ideal for processing objects and classes until PHP5 used Zend Engine2 (ZE2) so that PHP can contain advanced object-oriented features. This chapter introduces the object-oriented features of PHP5. At present, many companies propose this part when recruiting PHPer.

Although php was targeted before PHP5ObjectProgramming support,ObjectAnd class processing is not so ideal, until PHP5 uses Zend Engine2 (ZE2) to enable PHP to include advanced targetingObjectFeatures. This chapter introduces PHP5ObjectFeatures. At present, many companies are demanding this part when recruiting PHPer. Note that the Chinese version of this book isObject(Instantiating Objects)ObjectIn the materials read by the bloggers, this line is described as "instantiating"Object.

After learning this chapter, you need to understandObjectUse of the three features: inheritance, encapsulation, and polymorphism.

Encapsulation is to encapsulate objective things into abstract classes, and classes can encapsulate their own data and methods to only make trusted classes orObjectOperation to hide untrusted information.

There are three types of inheritance: Implementation inheritance, interface inheritance, and visual inheritance.

  1. Implementation Inheritance refers to the ability to use the attributes and methods of the base class without additional encoding;
  2. Interface inheritance only uses the names of attributes and methods, but the subclass must provide implementation capabilities;
  3. Visual Inheritance refers to the ability of sub-forms (classes) to use the appearance of base Forms (classes) and implement code.

There are two methods of polymorphism: overwrite and reload.

  1. Overwrite refers to the method by which the subclass redefines the virtual function of the parent class.
  2. Overload means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (maybe the number of parameters is different, maybe the parameter types are different, or both are different ).

Several special method names in the class

_ Construct: constructor. It is run first during class execution.

_ Destruct: destructor, run before the class ends

_ ToString: stringized. This method is different from PHP5.2 in earlier versions of PHP5.

Public, protected, private: Access Control, indicating public, protected, and private

Final: The subclass of a method that cannot be inherited or the class that cannot be inherited

Interface: defines an interface

Abstract: Define an abstract class

Clone: copy oneObjectCopy

$ Adam = new user; // two variables point to oneObject$ Dave = $ adam; // points to two different searches.Object$ John = clone $ adam;

_ Get ($ property) and _ set ($ property, $ value): attribute access

_ Isset () and _ unset (): determines whether to set or destroy it. It is only valid for Versions later than PHP5.1.

_ Call ($ method, $ arguments): Simply put, this parameter is called when an undefined method in the call class is called.

Parent: Method Name (): Call the method of the parent class in the subclass

Const: defines a class constant, which is called by [class name]: [constant name]

Static: defines static methods or attributes. NET into experience, you need to note that the static method in PHP is only valid in this request process, not like. NET static method will resident in the memory for a long time after one call.

_ Sleep () and _ wakeUp (): called when the serial number and deserialization of a class are used to save connections to some external resources.

Obtain the information of a class through reflection:

Reflection::export(new ReflectionClass('car'));

ReflectionMethod and ReflectionFunction: reflect a class method and reflect a function respectively.

Instanceof: detects oneObjectWhether it is an instance of the specified class

_ Autoload: This method is executed when a defined class is instantiated.

function __autoload($class_name) {    include "$class_name.php";}$person = new Person;

A tough PHP syntax

// Valid $ class_name = 'net _ ping'; $ class = new $ class_name; // invalid $ partial_class_name = 'ping' under new Net_Ping '; $ class = new "Net _ $ partial_class_name"; // new Net_Ping // valid $ partial_class_name = 'ping'; $ class_prefix = 'net _'; $ class_name = "$ class_prefix $ partial_class_name"; $ class = new $ class_name; // new Net_Ping

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.