PHP Object-oriented Programming _php tutorial

Source: Internet
Author: User
The concept of object-oriented programming:

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

Abstract data types and information encapsulation

Inherited

Polymorphic

In PHP, the encapsulation is done through a class:


Class Something {

In an OOP class, the first character is usually capitalized

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

Use the new operator to create an object:

$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 object's member variable x (not the Class), and then Getx returns its value 5. It is not a good OOP habit to access data members by way of a class reference like $obj->x=6. I strongly recommend accessing member variables through methods. If you consider the member variable to be non-processing and use the method only through the object handle, you will be a good OOP programmer. Unfortunately, PHP does not support declaring private member variables, so bad code is also allowed in PHP. Inheritance is easy to implement in PHP, as long as the Extend keyword is used.


Class Another extends Something {

var $y;

function Sety ($v) {

$this->y= $v;

}

function GetY () {

return $this->y;

}

}

Objects of 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 of the two classes. You can redefine a method in a derived class, and 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 that has the same name as implementation, 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, and is called when you create an object of a 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 automatically assigns a value of 6 to the data variable, x. Constructors and methods are normal 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 if the passed parameter is less than the required parameter, the default parameter is used.

When an object of a derived class is created, only its constructor is called, the constructor of the parent class is not called, and if you want to call 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 the derived class are available.


function another () {

$this->y=5;

$this->something ();

Show call base class constructor

}

A good mechanism for OOP is to use abstract classes. Abstract classes are not instantiated and can only be supplied to a derived class interface. Designers often use abstract classes to force programmers to derive from base classes, which ensures that new classes contain some expected functionality. There is no standard method in PHP, but: if you need this feature, you can define the base class, and add a "die" call after its constructor, so that the base class is not instantiated, and now after each method (interface) with a "Die" statement, so, If a programmer does not overwrite a method in a derived class, an error is raised. And because PHP is untyped, you might want to make sure that an object is derived from your base class, then add a method in the base class to the identity of the literal class (returning an Identity ID), and verify the value when you receive an object parameter. Of course, if an evil, bad programmer overrides this method in a derived class, this method does not work, but the general problem is that it is more of a lazy programmer than an evil one.

Of course, it's good for the base class to be impossible for programmers to see, as long as the interface is printed out to do their job. There are no destructors in PHP.

Overloads (unlike overrides) are 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 (this depends on the language). PHP is a loosely typed language, so it does not work with type overloading, but overloading with different number of parameters does not work.

Sometimes overloading constructors in OOP is great, so you can create objects in different ways (passing different numbers of parameters). The trick to implementing 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 the method that will be called

}

function Myclass1 ($x) {

Code

}

function Myclass2 ($x, $y) {

Code

}

}

By additional processing in the class, using this class is transparent to the user:

$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 determine the method of which object to invoke, depending on the passed object parameters 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 may 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.

http://www.bkjia.com/PHPjc/314285.html www.bkjia.com true http://www.bkjia.com/PHPjc/314285.html techarticle The concept of object-oriented programming: Different authors may disagree, but an OOP language must have the following aspects: Abstract data types and information encapsulation inheritance polymorphism in PHP ...

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