PHP object-oriented basic Note 2

Source: Internet
Author: User
1. What is inherited from PHP? A parent class can have only one parent class, and a parent class can have multiple child classes. Ii. Inheritance and overloading 3. Keywords 1. The Instanceof operator is used to detect whether the current object instance belongs to a class type. 2. The application of the Final keyword defines constants in other languages. In PHP, d

1. What is inherited from PHP? A parent class can have only one parent class, and a parent class can have multiple child classes. Ii. Inheritance and overloading 3. Keywords 1. The Instanceof operator is used to detect whether the current object instance belongs to a class type. 2. The application of the Final keyword defines constants in other languages. In PHP, d

1. What is inheritance?

In PHP, a parent class can have only one parent class, and a parent class can have multiple child classes.





Ii. Inheritance and overloading






3. Keywords

1. Instanceof Operator

Checks whether the current object instance belongs to a class type.




2. Application of Final keywords

In other languages, final defines constants.

In PHP, define defines constants, but final does not. You cannot use final to modify member attributes.

1) Final can modify the class-This class cannot be extended, it cannot be inherited by sub-classes, and the function cannot be extended. It is the Final class.

2) Final can modify the method ----- This method cannot be rewritten (overwritten) in the subclass ).

Objective: To ensure security, and to avoid necessity.





3. Use of Static keywords

1) if the class has the same attributes, you need to use them. Only one copy of the same attribute memory is saved. Initialize static segments, store static segments, and constants. Can be shared by all objects of the same class.

2) When the class is loaded to the memory, the static member has been added to the memory.

3) Static members must use classes for access. Is part of the class.

Public static $ country = "China ";

Echo person: $ country;



Self can represent its own class ($ this) in the method in the class)

Parent represents its Parent class during inheritance.

4) Once a static member is loaded, it is released only after the script is complete.

5) static function ceshi (){}

Student: ceshi ();

This method has the following advantages: you do not need to create an object.

Disadvantage: the script can be released only after it is completed, occupying the memory. In addition, you cannot access non-static members.

For example:





$ This indicates the current object. It cannot be used because no object exists.

As long as the method can be declared in a static environment, it is best to use the static method, considering the efficiency.

For example:



4. Const keywords

You cannot use define to define constants in a class. The member attributes modified by const are constants and can only be modified,

1) It is recommended that constants be capitalized and $.

2) constants must be declared with the initial value.

3) constant access is the same as static access, but can only be read. Outside the class, use: Class Name: constant name. Inside the class: self: constant name.


Iv. Single-state design mode (single-piece, single-instance)

This design mode is most suitable for php.

1. If you want a class to have only one object, you must first prevent this class from creating an object and private the constructor method, that is, private.

2. You can use a method in the class memory to create objects.

5. Magic method _ toString (). Returns a string

1. The common method in the class is to automatically call this method when an object reference is directly Output Using echo, print, and printf.

2. Place the basic information of the object in the _ toString () method to form a string to return.

3. In _ toString (), no parameter exists and a string must be returned.






6. Magic method__clone () clone object

1. An object is copied using the clone keyword.

1. The method is automatically called when the object is cloned.

2. Purpose: Call not a constructor, but initialize the new cloned object.

3. In the clone method, $ this represents a copy, so you can initialize all the members of the copy.




Output:




7. Magic method__ call () method handle call errors

1. Call a method that does not exist in an object automatically.

2. There are two parameters: one parameter is the name of the called method that does not exist, the other parameter is the method parameter that does not exist, and the parameter is an array.



3. You can write a prompt, but it is not the main function. The method functions are similar, but the method names are different. You can use this method.





8. Object serialization)

Object> string

1. Transfer objects over the network

2. Save the object for a long time

3. Implementation Method: system function serialize ()

// Convert an object to a string

$ Str = serialize ($ p );

// Write the string to the file

File_put_contents(“objstr.txt ", $ str );

4. deserialization

// Read the string from the file

$ Str = file_get_contents(objstr.txt );

// Deserialization

$ P = unserialize ($ str );

All serializing

1. serialize serializes the specified class Object $ str = serialize ($ per) // serializes the per object and returns the result to $ str

2. The returned result of unserialize is the object $ per = unserialize ($ str );

Local serialization

3. _ sleep () serializes some attributes of an object.

4. When _ wakeup () is deserialized, the object content is initialized (actually modified ).

We have already introduced the first two usage methods. Next we will briefly introduce _ sleep () and _ wakeup () usage methods.

1. if we only want to serialize some attributes of an object, we can use the _ sleep () function.

Add in class definition

Function _ sleep () // only serializes the name and age members in the class

{

$ Arr = newarray ('name', 'age'); name and age must be attributes in the class and can be added according to your actual needs.

Return arr;

}

2. What if we change the name attribute value of per object to "Zhang San" in deserialization when class serialization is made?

Function _ wakeup ()
{

This-> name = "James ";

}

9. Magic method _ set_state ()

Learning functions:

1. Eval () function-check and execute PHP code

$ Str = "echo 'abc ';";

Eval ($ str );

2. Var_dump (); function output print format information

3. Var_export ();

10. Magic method__autoload automatic loading class

This method is automatically called as long as the class name needs to be loaded in this script,


When defining the file name of a class, you need to follow certain rules. It is best to center the class name with a uniform prefix or suffix to form a file name.


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.