_php Foundation of object-oriented Programming in PHP

Source: Internet
Author: User
Tags abstract inheritance

The concept of object-oriented programming:

Different authors may say differently, but an OOP language must have the following:

Abstract data types and information encapsulation

Inherited

Polymorphic

The encapsulation is done in PHP by a class:

<?php

Class Something {

In an OOP class, the first character is usually uppercase

var $x;

function SetX ($v) {

Method starts with a lowercase word and then uses uppercase letters to separate words, such as Getvalueofarea ()

$this->x= $v;

}

function GetX () {

return $this->x;

}

}

Of course you can define your own preferences, but it's best to keep a standard, which is more effective. Data members are defined in a class using the "var" declaration, which is not typed until the data member is assigned a value. A data member can be an integer, an array, an associated array (associative array), or an object. method is defined as a function form in a class, you should use $this->name when accessing a class member variable in a method, otherwise it can only be a local variable for a method.

To create an object using the new operator:

$obj =new something;

You can then use the member function to pass:

$obj->setx (5);

$see = $obj->getx ();

In this example, the SETX member function assigns 5 to the member variable X (not the Class) of the object, and then Getx returns its value of 5. It is not a good OOP habit to access data members in the same way as $obj->x=6 through a class reference. I strongly recommend using methods to access member variables. You would be a good OOP programmer if you think of the member variables as not being handled and use the method only through the object handle. Unfortunately, PHP does not support declaring private member variables, so bad code is also allowed in PHP. Inheritance is easy to implement in PHP, just use the Extend keyword.

<?php

Class Another extends something {

var $y;

function Sety ($v) {

$this->y= $v;

}

function GetY () {

return $this->y;

}

}

Objects in the "Another" class now have all the data members and methods of the parent class (something), plus their own data members and methods.

You can use

$obj 2=new something;

$obj 2->setx (6);

$obj 2->sety (7);

PHP does not now support multiple inheritance, so you cannot derive new classes from two or more than two classes. You can redefine a method in a derived class, and if we redefine the Getx method in the "Another" class, we can't use the Getx method in "something". If you declare a data member with the same name as Kippe in a derived class, it will "hide" the data members of the base class when you process it.

You can define constructors in your class. A constructor is a method that has the same name as a class name and is invoked when you create an object of a class, for example:

<?php

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 automatically assigns a value of 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")

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 uses C + +, so you can't ignore the value of Y, and give X a default parameter, which is assigned from left to right, and uses default arguments if the incoming argument is less than the required parameter.

When an object of a derived class is created, only its constructors are invoked, the constructor of the parent class is not invoked, and if you want to invoke the constructor of the base class, you must display the call in the constructor of the derived class. You can do this because the methods of all the parent classes in a derived class are available.

<?php

function Another () {

$this->y=5;

$this->something ();

Show call base class constructor

}

An excellent mechanism for OOP is to use abstract classes. An abstract class is not instantiated and can only be supplied to a derived class as an interface. Designers often use abstract classes to force programmers to derive from a base class, which ensures that the new class contains some expected functionality. There is no standard method in PHP, but: if you need this feature, you can define the base class and add "die" to its constructor, which guarantees that the base class is not instantiated and now adds the "die" statement after each method (interface), so If a programmer does not overwrite a method in a derived class, an error is raised. And because PHP is untyped, you may need to verify that an object is derived from your base class, add a method to the base class's identity (return some identity id), and validate the value when you receive an object parameter. Of course, if an evil-bad programmer overrides this method in a derived class, this approach will not work, but the general problem is more of an idle programmer than an evil one.

Of course, it's nice to be able to make the base class impossible for programmers to see, just print the interface to do their job. There are no destructors in PHP.

Overloading (unlike overlay) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same name, but with different numbers or types of arguments (which depends on the language). PHP is a loosely typed language, so it does not work through type overloading, but overloading does not work with different numbers of arguments.

Sometimes overloading constructors in OOP is great, so you can create objects in different ways (passing a different number of arguments). The trick to implementing it in PHP is:

<?php

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 the method to be invoked

}

function Myclass1 ($x) {

Code

}

function Myclass2 ($x, $y) {

Code

}

}

Using this class is transparent to the user through additional processing in the class:

$obj 1=new Myclass (' 1 '); Will call Myclass1

$obj 2=new Myclass (' 1 ', ' 2 '); Will call Myclass2

Sometimes this is very useful.

Polymorphic

Polymorphism is the ability of an object to decide which object to invoke, based on the parameters of the object passed at run time. For example, if you have a figure class, it defines a draw method. and derive the circle and rectangle classes, in which you override the Draw method, you might also have a function that expects to use a parameter X and can call $x->draw (). If you have polymorphism, calling which draw method depends on the type of object you pass to the 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.