How to develop a large PHP project

Source: Internet
Author: User

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 of the OOP concepts and PHP techniques. Good luck!

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 by 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 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
Is an integer, an array, a related array (associative array), or an object.

method is defined as a function in a class, you should use $this->name when accessing class member variables in a method, otherwise
method, he can only be a local variable.

Use the new operator to create an object:

$obj =new something;

Then you can use the member function through:

$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 his value of 5.

It's not a good oop habit that you can access data members as you would $obj->x=6, by referencing them in a class. I strongly recommend that
Methods to access member variables. If you consider the member variable to be non-processing and use the method only through the object handle, you will be a
A good OOP programmer. Unfortunately, PHP does not support declaring private member variables, so bad code is also allowed in PHP.

Inheritance is very 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 currently 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, he will "hide" the data members of the base class when you work with him.

You can define constructors in your class. A constructor is a method that has the same name as a class name 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, if
When the parameter passed in is less than the required parameter, the default parameter is used.

When an object of a derived class is created, only his constructor is called, and the constructor of the parent class is not called, if you want to call the base
class, you must display the call in the constructor of the derived class. This can be done because the methods of all the parent classes in the derived class are
is available.
-----------------------------------------------------
function another () {
$this->y=5;
$this->something ();
Show call base class constructor
}

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

A very good mechanism for OOP is the use of abstract classes. Abstract classes are not instantiated and can only be supplied to a derived class interface. Designers usually
Using an abstract class to force a programmer to derive from a base class 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 to his constructor to ensure that the base
The class is not instantiated and currently has a "die" statement behind each method (interface), so if a programmer does not have a
Overriding the method, an error is raised. And because PHP is untyped, you might want to make sure that an object is derived from your base class.
Class, a method is added to the base class to implement the identity of the class (returning an Identity ID) and verifying when you receive an object parameter
This value. Of course, this method does not work if an evil, bad programmer overrides this method in a derived class, but generally
The problem is more of a lazy programmer than an evil programmer.

Of course, the ability to make the base class impossible for programmers to see is very good, 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 heavy methods with the same
Name, but there are different numbers or types of parameters (this depends on the language). PHP is a loosely typed language, so overloading by type does not
Works, but overloading by the number of parameters is not a function.

Sometimes overloading constructors in OOP is great, so you can create objects in different ways (passing different numbers of parameters). in PHP
In the implementation of his skills are:

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

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 (); Will call Myclass2

Sometimes this is very useful.

Cond...



This article comes to the source world http://www.ymsky.net/views/123334.shtml

  • Related Article

    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.