How to develop a large PHP project (i)

Source: Internet
Author: User
Tags abstract define array implement inheritance interface variables variable
Project This article describes object-oriented programming in PHP (Oop,object oriented programming). I'll show you how to reduce coding and improve quality by using some OOP concepts and PHP techniques. Good luck!





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


Inheritance


polymorphic





is encapsulated in PHP through a class:---------------------------------------------------<?php


class Something {


//In an OOP class, the first character is usually uppercase


var $x;


function SetX ($v) {

The
//method starts as a lowercase word, and then uses uppercase letters to separate the 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


to be an integer, an array, an associated array (associative array), or an object.




The
method is defined as a function form in a class, and when you access a class member variable in a method, you should use $this->name, or a side


method, it can only be a local variable.





use the new operator to create an object:





$obj =new something;





then you can 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.





You can access data members in the same way as $obj->x=6, which is not a good oop habit. I strongly recommend that you pass


methods are used to access member variables. If you consider a member variable to be
and use the method only through the object handle, you will be a

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











-----------------------------------------------------


<?php





class Another extends something {


var $y;


function Sety ($v) {


$this->y= $v;


}


function GetY () {


return $this->y;


}


}





?>---------------------------------------------------




The object of the
"Another" class now has all the data members and methods of the parent class (something), plus its 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, through:





$obj =new Something (6);




The
constructor automatically assigns 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 if


the parameters passed in are less than the required parameters, they will use the default parameters.





when an object of a derived class is created, only its constructors are invoked, and the constructor of the parent class is not invoked, if you want to call the base


class constructor, 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


is available.





-----------------------------------------------------


<?php





function Another () {


$this->y=5;


$this->something ();


//Display calls the base class constructor


}





?>---------------------------------------------------




A good 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 usually


uses 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 a "die" call after its constructor so that the base

The
class is not instantiated and is now preceded by a "die" statement after each method (interface), so if a programmer does not have
in a derived class

overrides method, an error is raised. And because PHP is untyped, you may need to verify that an object is derived from your base class


class, add a method in the base class to the identity of the real class (return some identity id) and validate
when you receive an object parameter

this value. Of course, if an evil-bad programmer overrides this method in a derived class, this method will not work, but the general

The
problem is more of a lazy programmer than an evil one.





of course, it's good to make the base class impossible for programmers to see, just print the interface to do their job.





there is no destructor 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


's name, but there are different numbers or types of arguments (this depends on the language). PHP is a loosely typed language, so the type overload does not


works, however, overloading does not work by varying the number of parameters.





sometimes overloading constructors in OOP is great, so you can create objects in different ways (passing a different number of arguments). in PHP

The trick to implement it in
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;


}


}





?>---------------------------------------------------





uses this class to be 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 easy to use.





to be Continued ...





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.