Special methods for large PHP projects

Source: Internet
Author: User
This article describes how to develop large PHP projects in php (oop and objectorientedprogramming ). I will show you how to reduce coding and improve quality by using some oop concepts and php skills. Good luck! The concept of object-oriented programming: different authors may have different ideas, but an oop language must have the following aspects: abstract data types and information encapsulation inheritance polymorphism is a method for developing large php projects in PHP.

This article introduces object-oriented programming (oop) in php ). I will show you how to reduce coding and improve quality by using some oop concepts and php skills. Good luck!

The concept of object-oriented programming:
Different authors may have different arguments, but an oop language must have the following aspects:

Abstract data types and information encapsulation
Inheritance
Polymorphism

In php, classes are encapsulated :--------------------------------------------------- Class something {
// In The oop class, the first character is usually uppercase.
Var $ x;
Function setx ($ v ){
// The method starts with lowercase words, and then uses uppercase letters to separate words, such as getvalueofarea ()
$ This-> x = $ v;
}
Function getx (){
Return $ this-> x;
}
}
?> ---------------------------------------------------

Of course you can define according to your preferences, but it is best to maintain a standard, which will be more effective.

Data members are defined using the "var" declaration in the class. they have no type before assigning values to data members. A data member can
It is an integer, an array, an associated array, or an object.

The method is defined as a function in the class. when using a member variable of the class in the method, you should use $ this-> name. otherwise
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 object's member variable x (not a class), and then getx returns its value 5.

It is not a good oop habit that you can access data members by referencing classes like $ obj-> x = 6. I strongly recommend
Methods to access member variables. If you think of the member variables as unprocessable and use the methods 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 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;
}
}

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

The objects of the "another" class currently have all the data members and methods of the parent class (something), and their own data members and methods are added.

You can use
$ Obj2 = new something;
$ Obj2-> setx (6 );
$ Obj2-> sety (7 );

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

You can define constructors in your class. A constructor is a method with the same name as a class name. it is called when you create a class object. for example:
-----------------------------------------------------

Class something {
Var $ x;

Function something ($ y ){
$ This-> x = $ y;
}

Function setx ($ v ){
$ This-> x = $ v;
}

Function getx (){
Return $ this-> x;
}
}

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

Therefore, you can create an object:

$ Obj = new something (6 );

The constructor automatically assigns value 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 ")
Next:
$ 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 the c ++ method, so you cannot ignore the value of y. Instead, assign a value to x from left to right. if
When the input 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, and the constructor of the parent class is not called.
Class constructor, you must display the call in the constructor of the derived class. This is because all methods of the parent class in the derived class have
Is available.
-----------------------------------------------------
Function another (){
$ This-> y = 5;
$ This-> something ();
// Display the call base class constructor
}

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

An excellent mechanism of oop is to use abstract classes. Abstract classes cannot be instantiated and can only be provided to an interface of the derived class. Designers usually
The abstract class is used to force the programmer to derive from the base class. This ensures that the new class contains some expected functions. There is no standard method in php,
However:

If you need this feature, you can define the base class and add the "die" call to its constructor to ensure that the base class
Classes cannot be instantiated. Currently, the "die" statement is added after each method (interface). Therefore, if a programmer does not
OverWrite method will cause an error. And because php is non-typed, you may need to confirm that an object is derived from your base class.
Class, then add a method to the base class to implement the class identity (return a certain id), and verify when you receive an object parameter
This value. Of course, if an evil programmer overwrites this method in a derived class, this method does not work, but generally
There are many problems with programmers who are currently lazy, rather than evil programmers.

Of course, it is very good to make the basic class invisible to programmers, as long as the interface is printed out for their work.

No Destructor in php.
Overloading (different from overwriting) is not supported in php. In oop, you can overload a method to implement two or more methods with the same
But there are different numbers or types of parameters (depending on the language ). Php is a loose type language.
However, the number of parameters does not work.

Sometimes the overload constructor in oop is very good, so that you can create objects through different methods (passing different numbers of parameters ). In php
Is:

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

Class myclass {
Function myclass (){
$ Name = "myclass". func_num_args ();
$ This-> $ name ();

// Note that $ this-> name () is generally incorrect, but $ name is the name of the method to be called.
}
Function myclass1 ($ x ){
Code;
}
Function myclass2 ($ x, $ y ){
Code;
}
}

?> ---------------------------------------------------
Through additional processing in the class, using this class is transparent to users:
$ Obj1 = new myclass (1); // call myclass1
$ Obj2 = new myclass (1, 2); // call myclass2

Sometimes this is very useful.

To be continued...



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


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.