Home  >  Article  >  Backend Development  >  Object-oriented programming in PHP_PHP tutorial

Object-oriented programming in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:10:17769browse


The concept of object-oriented programming:

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

Abstract data types and information encapsulation

Inheritance

Polymorphism

is encapsulated through classes in PHP:


class Something {

// In OOP classes, usually the first character is uppercase

var $x;

function setX($v) {

// The method starts in lowercase words, and then use capital letters to separate words, such as getValueOfArea()

$this->x=$v;

}

function getX() {

return $this->x;

}

}

Of course you can define it according to your own preferences, but it is best to maintain a standard. This will be more efficient. Data members are defined in the class using "var" declarations. Before data members are assigned a value, they have no type. A data member can be an integer, an array, an associative array, or an object. Methods are defined as functions in a class. When accessing class member variables in a method, you should use $this->name. Otherwise, for a method, it can only be a local variable.

Use the new operator to create an object:

$obj=new Something;

You can then use member functions via:

$obj- >setX(5);

$see=$obj->getX();

In this example, the setX member function assigns 5 to the object’s member variable x (not class), then getX returns its value 5. You can access data members through class references like: $obj->x=6. This is not a good OOP habit. I strongly recommend accessing member variables through methods. You will be a good OOP programmer if you treat member variables as unmanipulable and use methods only through object handles. Unfortunately, PHP does not support declaring private member variables, so bad code is allowed in PHP. Inheritance is easy to implement in PHP, just use the extend keyword.


class Another extends Something {

var $y;

function setY($v) {

$this->y=$v;

}

function getY() {

return $this->y;

}

}

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

You can use

$obj2=new Something;

$obj2->setX(6);

$obj2->setY( 7);

PHP currently does not support multiple inheritance, so you cannot derive new classes 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 in a derived class with the same name as the base class, it will "hide" the base class data member when you deal with it.

You can define constructors in your class. The constructor is a method with the same name as the class name, which is called when you create an object of the class, for example:


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 by:

$obj=new Something(6);

The constructor will automatically assign 6 to the data variable x. Constructors and methods are normal PHP functions, so you can use 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 default parameters use the C++ method, so you cannot ignore the value of Y and give a default parameter to X. The parameters are assigned from left to right. If the parameters passed in are less than When parameters are required, the default parameters will be used.

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 do it in the derived class. Explicit call in constructor. This can be done because all methods of the parent class are available in the derived class.


function Another() {

$this->y=5;

$this->Something() ;

//Explicitly calling the base class constructor

}

A good mechanism for OOP is to use abstract classes. Abstract classes cannot be instantiated and can only provide an interface to derived classes. Designers often use abstract classes to force programmers to derive from a base class, thus ensuring that the new class contains some desired functionality. There is no standard method in PHP, but: if you need this feature, you can define a base class and add a "die" call after its constructor, thus ensuring that the base class is not instantiable. Now Add a "die" statement after each method (interface), so if a programmer does not override the method in a derived class, an error will be raised. And because PHP is untyped, you may need to confirm that an object is a derived class from your base class, then add a method in the base class to define the identity of the class (return some kind of identification id), and in your Check this value when receiving an object parameter. Of course, if an evil programmer overrides this method in a derived class, this method will not work, but generally the problem is found in lazy programmers, not evil programmers.

Of course, it's nice to be able to keep the base classes invisible to programmers, who can just print out the interfaces and do their job. There is no destructor in PHP.

Overloading (unlike overriding) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same name but different numbers or types of parameters (depending on the language). PHP is a loosely typed language, so overloading by type does not work, but overloading by different number of parameters does not work either.

Sometimes it's good to overload constructors in OOP so that you can create objects through different methods (passing different numbers of arguments). The trick to implement it in PHP is:


class Myclass {

function Myclass() {

$name="Myclass ".func_num_args();

$this->$name();

//Note that $this->name() is generally wrong, but here $name is The name of a method to be called

}

function Myclass1($x) {

code;

}

function Myclass2 ($x,$y) {

code;

}

}

Use this class to benefit users through additional processing in the class Is transparent:

$obj1=new Myclass('1'); //Myclass1 will be called

$obj2=new Myclass('1','2'); //Will be called Call Myclass2

Sometimes this is very useful.

Polymorphism

Polymorphism is an ability of an object. It can decide which object method to call based on the passed object parameters at runtime. For example, if you have a figure class, it defines a draw method. And derived the circle and rectangle classes, in the derived class you override the draw method, you may also have a function that expects a parameter x, and can call $x->draw(). If you have polymorphism, which draw method is called depends on the type of object you pass to the function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314285.htmlTechArticleThe concept of object-oriented programming: different authors may have different opinions, but an OOP language must have the following Aspects: Abstract data types and information encapsulation inheritance polymorphism in PHP...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn