(GO) PHP Object-oriented learning notes

Source: Internet
Author: User
Tags string back

1, write the main points of the class:

The class name should be descriptive and meaningful

Camel-like name: Class girlfriend

2. variables within the class--member properties

function within a class--member method

3, in the class of member properties must have a modifier, if you do not know what the modifier, you can use the var (keyword), if there are other modifiers do not have Var

4, construction method __construct ();

Ⅰ, the first method that is automatically called after the object creation is completed (special)

Ⅱ, method name is special, can be the same as the class name of the method name

Ⅲ, assigning an initial value to a member of an object using the

5, the Destruction Method __destruct ();

Automatically called before an object is destroyed

Cannot have any arguments

Primarily used to release resources (such as closing files, releasing result sets, etc.)

6, Magic Method:

__get ()

1, Automatic call: is in direct access to private members, the automatic call! A parameter

__set ()

1, Automatic call: is directly set the value of the private property when the automatic call! Two parameters

__isset () isset () automatically calls the __isset () Magic method when using Isset () to determine if a private property exists, the parameter is the property name

__unset () unset () automatically calls the __unset () Magic method when using unset () to delete a private member, the parameter is the property name

Three features of object-oriented programming thought: encapsulation, inheritance, polymorphism

7. Inheriting---extension extends

① subclasses use extends to inherit the parent class, and subclasses can inherit all the content from the parent class.

②private are private, can only be used by themselves, not others, including their own sub-class can not be used

③protected This is the privilege of protection, can only be used in its own and its own subclasses of the members, cannot be used outside

④public This is public permission, all can, itself and subclass, the outside of the class can be used

8. Common key Words

instanceof

The "instanceof" operator is used to detect whether the current object instance belongs to a class type

Final

Final does not define constants in PHP, so it is not used and cannot be modified with final to modify member properties

1, final can be modified class-this class can not be extended, cannot have sub-class (Do not let others expand, this class is the final Class)

2, Final can be modified method-this method can not be overridden in subclasses (do not let subclasses to change this method, or extend the function of this method, this method is the final method)

Static

Static can modify properties and methods, cannot decorate classes,

1, use static modifier member property, there is the initialization of memory static segment

2. Can be shared by all objects of the same class

3, the first time to use the class (the first time the class name appears), the class is loaded into memory, the static member has been added to the memory

Objects, Members

Class:: Member

4. Static members must use classes to access

5, self can be in the method of the class, representing their own class ($this)

6. Once the static member is loaded, only the end of the script will be released.

7. Non-static members cannot be accessed in a static method

8, as long as the use of static environment to declare the method, it is best to use static method (mainly consider efficiency)

9, single-state (single, single-piece) design mode-best for PHP to use this design mode

1, if you want a class can have only one object, it is necessary to let this class can not create objects, the construction method private

2, you can use a static method inside the class to create the object

10. Serialization of Objects (serialized)

1. Turn the object into a string (do not read)---serialization (serialize)

2. Crossdress the string back to the object---(unserialize)

Note (the timing of serialization):

1. Transfer objects to the network

2. Persist the object

Array serialization JSON--JavaScript object

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

Var_dump ()

Var_export ()--Returns the structure information about the variable passed to the function

The __set_state () Magic method is the method (static, public) that is automatically called when the information of a class is exported when the Var_export () method is used.

__invoke () Magic method, is after the object instance, directly like the variable function call, automatically call this method (static, public) php5.3 after the new addition method

The __call () Magic method, which calls this method automatically when a method is called that does not exist

The __callstatic () Magic method, which calls this method automatically (static, public) when a static method is called that does not exist

__autoload () Magic method, can be written outside the class, as long as in this script, need to load the class (must use the class name), it will automatically call this method

11. Abstract methods and abstract classes

1. What is an abstract method?

Definition: A method If there is no method body (a method, do not use "{}", directly using the semicolon end method, is the method without the method body), this method is an abstract method

One, declare a method, do not use {}, and directly end with a semicolon.

Second, if it is an abstract method, it must be decorated with abstract keywords.

2. What is abstract class?

If one of the methods in a class is an abstract method, then this class is an abstract class

Second, if an abstract class is declared, the class must be decorated with the abstract keyword

Note 1:

1. If you use abstract to modify this class, this class is abstract class

2, abstract class is a special kind of class, special where (in abstract class can have abstract method)

3, except in the abstract class can have abstract methods, and normal exactly the same

NOTE 2:

1. An abstract class cannot instantiate an object, that is, an abstract class cannot create an object

2. If you see an abstract class, you must write the subclass of the class, overriding the abstract method in the abstract class (plus the method body)

3, subclasses must implement all (overwrite rewrite) abstract method, this subclass can create object, if only implement part, then there is abstract method, then the class must be abstract class

The role of abstract methods:

The function of the abstract method is to stipulate that the subclass must have the implementation of this method, the function gives the subclass

Write only the structure, but not the implementation, the implementation to the specific subclass (according to their own function) to achieve

The role of abstract classes:

is to require the structure of a subclass, so an abstract class is a specification

Abstract class person{

Public $name;

Public $sex;

Abstract methods

abstract function say ();

Abstract function eat ();

Public Function run () {

Echo ' Run ';

}

Public Function sleep () {

echo ' Sleep ';

}

}

12. Interface technology in object-oriented

Abstract class is a special class, interface is a special kind of abstract class, so the interface is a special special class

1. Abstract classes and interfaces have abstract methods

2. Abstract classes and interfaces cannot create instance objects

3, the use of abstract class and interface meaning is the same role

What is special about interfaces compared to abstract classes?

1, the method in the interface, must be all abstract method (cannot use non-abstract method)

Therefore, abstract methods in the interface do not need to use abstract, directly using the semicolon end can

2. The member property in the interface must be a constant and cannot have a variable

3. All permissions in the interface must be shared (public)

4, the Declaration interface does not use the class, but uses the interface

Some details of the interface application:

1, can use extends, let one interface inherit another interface (interface and interface--only extend new abstract method, no overwrite relationship)

2, you can use a class to implement all the methods in the interface, you can also use an abstract class to implement some of the methods in the interface

(Classes and interfaces, abstract classes and interfaces--overrides--overrides, implements abstract methods in the interface)

3, do not use extends keyword, using implements implementation, implements equivalent to extends

Extends inheritance (extension), this in PHP, a class can have only one parent class

4, a class can inherit another class, the use of implements implementation of an interface, you can implement multiple interfaces (must first inherit, and then implement the interface)

5, implementation of multiple interfaces, only need to separate multiple interfaces with commas can be

Declares an interface to use interface

Interface demo{

Const NAME = ' Zhang San ';

Const SEX = ' Male ';

function test1 ();

function Test2 ();

function Test3 ();

}

13. The characteristic polymorphism of the PHP surface image Object

Polymorphic Properties

1, Program expansion preparation

Technology:

1, must have an inheritance relationship, the parent class is preferably an interface or abstract class

14, namespace--name space

1. Constants

2. Functions

3. Class

One, namespace to declare

Second, in the namespace declaration namespace code above, cannot have any PHP code and the HTML content output (except declare), declares the namespace only is the first

Iii. define multiple namespaces, whichever is the last

Iv. in practical programming practice, it is highly discouraged to define multiple namespaces in the same file (do not add any code outside the curly braces)

(GO) PHP Object-oriented learning notes

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.