Method for developing large PHP projects (1)

xiaoxiao2021-03-06  44

Author: Luis Argerich Translator: limodou

This article describes object-oriented programming in PHP (OOP, Object Oriented Programming). I will demonstrate how to reduce encoding and improve quality by using some OOP concepts and PHP skills. good luck!

Object-oriented programming:

Different authors may not be the same, but an OOP language must have the following aspects:

Abstract data type and information package

inherit

Polymorphism

In the PHP, it is passed through class:

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

Class Something {

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

VAR $ x;

Function setX ($ V) {

/ / The method begins to lowercase words, then use 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 is best to maintain a standard, which will be more effective.

Data members are defined using the "VAR" declaration in the class. They are not type before assigning the data member. A member of a data

To be an integer, an array, a associated array (Associative Array) or an object.

The method is defined in the form of a function in the class. When you access a class member variable 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 assignments to the object of the object X (not class), and then getx returns its value 5.

You can use: $ OBJ-> X = 6 to access data members through class reference methods, which is not a good OOP habit. I strongly recommend access to member variables through way. If you regard the member variables as uncomfortable, you will be a good OOP programmer by using the object handle. Unfortunately, PHP does not support statement of private member variables, so bad code is also allowed in PHP.

Inheriting is easy to implement in PHP, just use the extend keyword.

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

Class Another Extends Something {

Var $ y;

Function SetY ($ V) {

$ this-> y = $ V;

}

Function game () {

Return $ this-> y;

}

}

?>

转载请注明原文地址:https://www.9cbs.com/read-112378.html

New Post(0)