Implementing object-oriented Programming in PHP (top)

Source: Internet
Author: User
Tags abstract define array functions inheritance variables variable access
Programming | Object This article describes object-oriented programming (OOP) in PHP. I'll show you how to use object-oriented concepts to make fewer code but better programs. Good luck to all of you.

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

-Data abstraction and information hiding
-Inherit
-Polymorphism

Ways to encapsulate a class in PHP:

Class Something {
In the OOP classes are usually named starting with a caps letter.
var $x;

function SetX ($v) {
Methods start in lowercase then with 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 members are not typed until they are assigned. A data member may be an integer, an array, a union array (associative array), or even an object. method to define a function in a class, to access data members in a method, you must use the $this->name method, otherwise it is a local variable for the 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 object (instead of the Class) member variable in obj, and then GetX returns the value 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. You should use a member function to set the value of a member variable and to read a member variable by using a 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 with lowercase to seperate
Words in the method name Example Getvalueofarea ()
$this->y= $v;
}

function GetY () {
return $this->y;
}
}

? >
Such "Another" objects have the member variables and method functions of 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 allow a class to inherit multiple classes.

You can redefine the method in the inheriting class, and if we redefine the GetX in "Another", then we no longer have access to 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, then the variable of the inheriting class hides the same variable of the parent class.

You can define the constructor of a class, which is a member function with the same name as the class, which is invoked when you create the 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;
}
}

? >
So you can create an object in the following ways:

$obj =new Something (6);
constructor automatically assigns a value of 5 to member variable x, constructors and member functions are normal PHP functions, so you can use the default arguments.

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 parameters are defined in the same way as C + +, so you can't pass a value to Y but let X take the default, the argument passes from left to right, and when there are no more arguments, the function uses the default 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 invoked, which is a feature of PHP's different object-oriented languages, because the constructor call chain is the characteristic of object-oriented programming. If you want to call the constructor of the base class, you have to call it explicitly in the constructor of the inheriting class. This makes it 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 to use abstract classes, which are classes that cannot be instantiated but are used to define an interface for an inherited class. Designers often use abstract classes to force programmers to inherit only from a particular base class, so they can determine what functionality is required for a 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 call "die" in the constructor, so you can make sure that it cannot be instantiated, now define the function of the abstract class and Invoke "die" in each function, if the programmer does not want to redefine the function of the base class directly in the inheriting class, an error will occur.

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

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

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 parameters). A small door can do this:

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

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

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

$obj 1=new Myclass (1); would call Myclass1
$obj 2=new Myclass (1,2); would call Myclass2
I feel good!


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.