PHP Object-Oriented Programming

Source: Internet
Author: User


The concept of object-oriented programming:

Different authors may have different ideas, but an OOP language must have the following aspects:

ABSTRACT Data Types and information Encapsulation

Inheritance

Polymorphism

In PHP, classes are encapsulated:

<? PHP

Class something {

// In the OOP class, the first character is usually uppercase.

VaR $ X;

Function setx ($ v ){

// The method starts with lowercase words, and then uses uppercase letters to separate words, such as getvalueofarea ()

$ This-> X = $ V;

}

Function getx (){

Return $ this-> X;

}

}

Of course, you can define it according to your preferences, but it is better to maintain a standard to make it more effective. Data members are defined using the "Var" declaration in the class. They have no type before assigning values to data members. A data member can be an integer, an array, an associated array, or an object. A method is defined as a function in a class. When using a member variable of the class in a method, you should use $ this-> name. Otherwise, a method can only be a local variable.

Use the new operator to create an object:

$ OBJ = new something;

Then you can use the member function to pass:

$ Obj-> setx (5 );

$ See = $ obj-> getx ();

In this example, the setx member function assigns 5 to the object's member variable X (not a class), and then getx returns its value 5. It is not a good OOP habit to access data members by referencing classes like $ obj-> X = 6. I strongly recommend that you use methods to access member variables. If you think of member variables as unprocessable and use methods only through object handles, you will be a good OOPProgramMember. Unfortunately, PHP does not support declaring private member variables.CodeThis is also allowed in PHP. Inheritance is easy to implement in PHP, as long as the extend keyword is used.

<? PHP

Class another extends something {

VaR $ Y;

Function sety ($ v ){

$ This-> Y = $ V;

}

Function Gety (){

Return $ this-> Y;

}

}

The object of the "another" class now has all the data members and methods of the parent class (something), and adds its own data members and methods.

You can use

$ Obj2 = new something;

$ Obj2-> setx (6 );

$ Obj2-> sety (7 );

PHP currently does not support multi-inheritance, so you cannot derive a new class from two or more classes. You can redefine a method in a derived class. If we redefine the getx method in the "another" class, we cannot use the getx method in "something. If you declare a data member with the same name as the base class in the derived class, it will "hide" The data member of the base class when you process it.

You can define constructors in your class. A constructor is a method with the same name as a class name. It is called when you create a class object. For example:

<? PHP

Class something {

VaR $ X;

Function something ($ Y ){

$ This-> X = $ Y;

}

Function setx ($ v ){

$ This-> X = $ V;

}

Function getx (){

Return $ this-> X;

}

}

Therefore, you can create an object:

$ OBJ = new something (6 );

The constructor automatically assigns value 6 to the data variable X. Constructors and methods are common PHP functions, so you can use the default parameters.

Function something ($ x = "3", $ Y = "5 ")

Next:

$ OBJ = new something (); // X = 3 and Y = 5

$ OBJ = new something (8); // X = 8 and Y = 5

$ OBJ = new something (8, 9); // X = 8 and Y = 9

The default parameter uses the C ++ method, so you cannot ignore the value of Y. Instead, assign a value to X from left to right, if the input parameter is less than the required parameter, the default parameter is used for the input parameter.

When an object of A derived class is created, only its constructor is called, and the constructor of the parent class is not called. If you want to call the constructor of the base class, you must display the call in the constructor of the derived class. This can be done because all methods of the parent class in the derived class are available.

<? PHP

Function another (){

$ This-> Y = 5;

$ This-> something ();

// Display the call base class Constructor

}

A good mechanism of OOP is to use abstract classes. Abstract classes cannot be instantiated and can only be provided to an interface of the derived class. Designers usually use abstract classes to force programmers to derive from the base class. This ensures that the new class contains some expected functions. There is no standard method in PHP, but if you need this feature, you can define the base class and add the "die" call after its constructor, this ensures that the base class cannot be instantiated. Now, the "die" statement is added after each method (interface). Therefore, if a programmer does not overwrite the method in the derived class, an error is thrown. And because PHP is non-typed, you may need to confirm that an object comes from the derived class of your base class, then add a method to the base class to implement the Class Identity (return a certain ID), and verify this value when you receive an object parameter. Of course, if an evil and bad programmer overwrites this method in a derived class, this method does not work, but generally the problem occurs frequently in the lazy programmer, rather than the evil programmer.

Of course, it is good to make the base class invisible to programmers, as long as the interface is printed out for their work. No destructor in PHP.

Overload (different from overwriting) is not supported in PHP. In Oop, You can overload a method to implement two or more methods with the same name, but there are different numbers or types of parameters (depending on the language ). PHP is a loose language, so the type overload does not work, but the overload does not work by the number of parameters.

Sometimes the overload constructor in OOP is very good, so that you can create objects using different methods (passing different numbers of parameters ). The tips for implementing it in PHP are:

<? PHP

Class myclass {

Function myclass (){

$ Name = "myclass". func_num_args ();

$ This-> $ name ();

// Note that $ this-> name () is generally incorrect, but $ name is the name of the method to be called.

}

Function myclass1 ($ X ){

Code;

}

Function myclass2 ($ X, $ Y ){

Code;

}

}

Through additional processing in the class, using this class is transparent to users:

$ Obj1 = new myclass ('1'); // call myclass1

$ Obj2 = new myclass ('1', '2'); // call myclass2

Sometimes this is very useful.

Polymorphism

Polymorphism is a capability of an object. It determines the method of the object to be called at runtime Based on the passed object parameters. For example, if you have a figure class, it defines a draw method. The circle and rectangle classes are derived. In the derived class, you overwrite the draw method. You may also have a function that uses the X parameter, you can call $ X-> draw (). If you have polymorphism, the draw method that you call depends on the object type that you pass to this function.

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.