Implement object-oriented programming in PHP (I)

Source: Internet
Author: User
This article introduces the implementation of object-oriented programming (OOP) in PHP ). I will demonstrate how to use object-oriented concepts to compile less code but better programs. Good luck. <Br/> <This article describes object-oriented programming (OOP) in PHP ). I will demonstrate how to use object-oriented concepts to compile less code but better programs. Good luck.

The concept of object-oriented programming has different views for each author. I would like to remind you of the following:

-Data abstraction and information hiding
-Inheritance
-Polymorphism

How to encapsulate classes in PHP:

Class Something {
// In OOP classes are usually named starting with a cap letter.
Var $ x;

Function setX ($ v ){
// Methods start in lowercase then use lowercase to seprate
// Words in the method name example getValueOfArea ()
$ This-> x = $ v;
}

Function getX (){
Return $ this-> x;
}
}

?>
Of course you can use your own method, but there is always a standard.

The data member of the class in PHP is defined by "var". The data member is of no type until it is assigned a value. A data member may be an integer, array, associative array, or even an object ). A method is defined as a function in a class. to access data members in a method, you must use $ this-> name. Otherwise, a method is a local variable of a function.

Use new to create an object

$ Obj = new Something;
Then use the member function

$ Obj-> setX (5 );
$ See = $ obj-> getX ();
The setX member function assigns 5 to the member variables in the object (rather than the class) obj, and then returns the value of getX 5.

You can also use object references to access member variables, such as $ obj-> x = 6. However, this is not a good object-oriented programming method. I insist that you should use the member function to set the value of the member variable and read the member variable through the member function. If you think that member variables are unaccessable, in addition to using member functions, you will become a good object-oriented programmer. Unfortunately, PHP itself cannot declare that a variable is private, so bad code is allowed.

In PHP, extend is used for inheritance.

Class Another extends Something {
Var $ y;
Function setY ($ v ){
// Methods start in lowercase then use lowercase to seperate
// Words in the method name example getValueOfArea ()
$ This-> y = $ v;
}

Function getY (){
Return $ this-> y;
}
}

?>
In this way, the object of the "Another" class has the member variables and method functions used by the parent class, plus its own member variables and member functions. For example:

$ Obj2 = new Another;
$ Obj2-> setX (6 );
$ Obj2-> setY (7 );
Multi-inheritance is not supported, so you cannot let a class inherit multiple classes.

You can redefine the method in the inheritance class. if we redefine getX in "Another", we will no longer be able to access the member function getX in "Something. similarly, if we declare a member variable with the same name as the parent class in the inherited class, the variable that inherits the class will hide the variable with the same name as the parent class.

You can define a class constructor. The constructor is a member function with the same name as the class and is called when you create class objects.

Class Something {
Var $ x;

Function Something ($ y ){
$ This-> x = $ y;
}

Function setX ($ v ){
$ This-> x = $ v;
}

Function getX (){
Return $ this-> x;
}
}

?>
You can use the following method to create an object:

$ Obj = new Something (6 );
The constructor automatically assigns 5 values to the member variable x. The constructor and member functions are common PHP functions, so you can use the default parameters.

Function Something ($ x = "3", $ y = "5 ")
Then:

$ 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 definition method of the default parameter is the same as that of C ++. Therefore, you cannot pass a value to Y but let X take the default value. The real parameter is passed from left to right, if no more arguments are available, the function uses the default parameters.

Only when the constructor of the inherited class is called Can the object of the inherited class be created, and the constructor of the parent class is not called. this is a feature of different PHP object-oriented languages, because the constructor call chain is a feature of object-oriented programming. If you want to call the base class constructor, you have to explicitly call it in the constructor of the inherited class. This method works because all methods of the parent class are available in the inherited class.

Function Another (){
$ This-> y = 5;
$ This-> Something (); // explicit call to base class constructor.
}

?>
In object-oriented programming, a good mechanism is to use abstract classes. abstract classes are classes that cannot be instantiated but are used to define interfaces for inherited classes. Designers often use abstract classes to force programmers to inherit only from a specific base class, so they can determine that the new class has the required functions, but there is no standard way to do this in PHP, however:

If you need this feature to define the base class, you can call "die" in the constructor to ensure that it cannot be instantiated, now define the abstract class function and call "die" in each function. if the programmer in the inherited class directly calls the base class function without redefinition, an error will be generated.

In addition, you need to be sure that because PHP has no type, some objects are created from the inheritance class inherited from the base class, therefore, add a method in the base class to identify the class (return "some identifiers") and verify this, when you receive an object as a parameter. But it is useless for a villain program, because he can redefine this function in the inheritance class, usually this method only works for the lazy programmer. Of course, the best way is to prevent the program from accessing the code of the base class and only provide the interface.

Overload is not supported in PHP. In object-oriented programming, you can reload a member function with the same name by defining different parameter types and quantity. PHP is a loose type language, so it is useless to reload the parameter type, and the same number of parameters cannot be reloaded.

Sometimes, overload constructors are useful in object-oriented programming, so you can create different objects in different ways (by passing different parameter numbers ). A clever man can do this:

Class Myclass {
Function Myclass (){
$ Name = "Myclass". func_num_args ();
$ This-> $ name ();
// Note that $ this-> $ name () is usually wrong but here
// $ Name is a string with the name of the method to call.
}

Function Myclass1 ($ x ){
Code;
}
Function Myclass2 ($ x, $ y ){
Code;
}
}

?>
This method can partially achieve the purpose of overloading.

$ Obj1 = new Myclass (1); // Will call Myclass1
$ Obj2 = new Myclass (1, 2); // Will call Myclass2
Not bad!

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.