_php tutorial for implementing object-oriented programming in PHP

Source: Internet
Author: User
This article describes object-oriented programming (OOP) in PHP. I'll show you how to use object-oriented concepts to compile less code but better programs. Good luck to all of you.

The concept of object-oriented programming has a different view for each author, and I remind you of what an object-oriented language should be:

-Data abstraction and information hiding
-Inheritance
-Polymorphism

Ways to encapsulate a class in PHP:

Class Something {
In OOP classes is 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 good standard.

The data members of a class in PHP use the "var" definition, and the data member is not typed until it is assigned a value. A data member may be an integer, an array, a union array (associative array), or even an object. method is defined in the class as a function, in the method to access data members, you must use $this->name, otherwise, the method is a function of the local variables.

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 variable in the object (not the Class) obj, and then GetX returns the value 5.

You can also use object references to access member variables, for example: $obj->x=6; However, this is not a good method of object-oriented programming. I insist. You should use the member function to set the value of the member variable and to read the member variable through the member function. If you think that member variables are not accessible, in addition to using member functions, you will become a good object-oriented programmer. Unfortunately, PHP itself has no way of declaring that a variable is private, so it allows bad code to exist.

Inheritance in PHP is declared using extend.

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;
}
}

? >

Such objects such as "another" have member variables and method functions that are used by the parent class, plus their own member variables and member functions. Such as:

$obj 2=new another;
$obj 2->setx (6);
$obj 2->sety (7);

Multiple inheritance is not supported, so you cannot have a class inherit multiple classes.

You can redefine the method in the inheriting class, and if we redefine the GetX in "another", then we can no longer access the member function GetX in "Something". Similarly, if we declare a member variable with the same name as the parent class in the inheriting class, the variable that inherits the class will hide the variable with the same name as the parent class.

You can define a class's constructor, which is a member function that has the same name as the class, and is called when you create an object of the class.

Class Something {
var $x;

function Something ($y) {
$this->x= $y;
}

function SetX ($v) {
$this->x= $v;
}

function GetX () {
return $this->x;
}
}

? >

This news a total of 4 pages, currently on the 1th Page 1 2 3 4


So you can create objects in the following ways:

$obj =new Something (6);

The constructor automatically assigns a value of 5 to the member variable x, the constructor and member functions are normal PHP functions, so you can use the default parameters.

function Something ($x = "3", $y = "5")

And 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 default parameter is defined in the same way as C + +, so you cannot pass a value to Y but let X take the default value, the pass of the argument is left to right, and the function uses the default parameter when there are no more arguments.

Only when the constructor of the inheriting class is called, the object of the inheriting class is created, and the constructor of the parent class is not called, which is the feature of PHP's different object-oriented languages, because the constructor call chain is an object-oriented programming feature. If you want to call the constructor of the base class, you have to explicitly call it in the constructor of the inheriting class. This allows it to work because the methods of the parent class in the inheriting class are all available.

function another () {
$this->y=5;
$this->something (); Explicit call to base class constructor.
}

? >

A good mechanism in object-oriented programming is the use of abstract classes, which are classes that cannot be instantiated but are used to define interfaces for inheriting classes. Designers often use abstract classes to force programmers to inherit only from specific base classes, so they can determine what functionality is required for the new class, but there is no standard way to do this in PHP, but:

If you are defining a base class that requires this feature, you can make sure that it cannot be instantiated by invoking "die" in the constructor, and now define the function of the abstract class and call "die" in each function, and if the programmer does not want to redefine the function of the base class directly in the inheriting class, it will produce an error.

In addition, you need to be sure that because PHP has no type, some objects are created from inherited classes inherited from the base class, so add a method in the base class to discern the class (return "some identities") and verify this, when you receive an object as a parameter to come in handy. But it doesn't work for a rogue program because he can redefine the function in an inherited class, which usually works only for lazy programmers. Of course, the best way is to prevent the program from contacting the base class code that only provides the interface.

Overloading is not supported in PHP. In object-oriented programming, you can overload a member function with the same name by defining different parameter types and how much. PHP is a loosely typed language, so parameter type overloading is useless, and overloading of the same number of different parameters does not work.

Sometimes it is useful to overload constructors in object-oriented programming, so you can create different objects in different ways (by passing different number of arguments). A small door can do this:

Class Myclass {
function Myclass () {
$name = "Myclass". Func_num_args ();
$this-> $name ();
Note that $this-> $name () is usually wrong
$name is a string with the name of the.
}

function Myclass1 ($x) {
Code
}
function Myclass2 ($x, $y) {
Code
}
}

? >

This method can partially achieve the purpose of overloading.

$obj 1=new Myclass (1); would call Myclass1
$obj 2=new Myclass (); would call Myclass2

This news a total of 4 pages, currently on the 2nd Page 1 2 3 4


polymorphism

polymorphism is defined as when an object is passed as a parameter at run time, the object can determine the ability to invoke that method. For example, using a class to define the method "draw", the inheriting class redefined the "draw" behavior to draw a circle or a square, so you have a function with the parameter x, which can be called $x->draw () in the function. If polymorphism is supported, then the call to the "draw" method depends on the type of Object X. Polymorphism is naturally supported in PHP (think of this case

http://www.bkjia.com/phpjc/532649. HTML www.bkjia.com true http://www.bkjia.com/phpjc/532649.html techarticle This article describes object-oriented programming (OOP) in PHP. I'll show you how to use object-oriented concepts to compile less code but better programs. Good luck to all of you. An overview of object-oriented programming ...

  • 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.