_php tutorial on object-oriented design of PHP learning notes

Source: Internet
Author: User
Object-oriented design is a very important part of PHP program development, if you want to be a senior PHP programmer, we must know the specific usage and wording of object-oriented design.

Maintaining a simple modularity is a feature in object-oriented programming. Entities are represented as classes and classes with the same functionality in the same namespace, we can add a class in the namespace without affecting other members of that namespace.

Extensible object-oriented programming essentially supports extensibility. If you have a class with some kind of functionality, you can quickly expand the class to create a class with expanded functionality.

Code reuse because functionality is encapsulated in a class, and the class exists as an independent entity, providing a class library is straightforward.

It is more suitable for multi-person cooperation to develop projects, so now many large and medium-sized sites have chosen to use OOP to develop.

This article mainly explains the basic methods and code examples of object-oriented programming with PHP, how to create a class and how to generate an instance of class, and so on, just a primer, very simple, to learn this is far from enough. Only for beginners in PHP

Public represents the global, and the inner and outer subclasses of the class can be accessed;

The code is as follows Copy Code

Class test{
Public $name = ' janking ',
$sex = ' Male ',
$age = 23;

function __construct () {
echo $this->age. '
'. $this->name. '
'. $this->sex. '
';
}

function func () {
echo $this->age. '
'. $this->name. '
'. $this->sex. '
';
}
}


$P =new Test ();
Echo '

';
$P->age=100;
$P->name= "Rainy";
$P->sex= "female";
$P->func ();
?>

Private means that only this class can be used internally;

code as follows copy code



Class test{
Private $name = ' janking ',
$sex = ' Male ',
$age = 23;

function __construct () {
$this->funcone ();
}

function func () {
echo $this->age. '
'. $this->name. '
'. $this->sex. '
';
}

Private Function Funcone () {
echo $this->age. '
'. $this->name. '
'. $this->sex. '
';
}
}


$P =new Test ();
Echo '

';
$P->func ();
$P->age=100; Cannot access private property Test:: $age
$P->name= "Rainy"; Cannot access private property Test:: $name
$P->sex= "female"; Cannot access private property Test:: $female
$P->funcone (); Call to Private Method Test::funcone () from context '
?>

Protected is protected and is accessible only in this class or subclass or in the parent class;

-Data abstraction and information hiding

-Inheritance

-Polymorphism

Ways to encapsulate a class in PHP:

The code is as follows Copy Code

Class Something {
In OOP classes is usually named starting with a cap letter.
var $x;

function SetX ($v) {
Methods start in lowercase then use lowercase to seprate
Words in the method name Example Getvalueofarea ()
$this->x= $v;
}

function GetX () {
return $this->x;
}
}

? >

  

Of course you can use your own method, but there is always a good standard.

The data members of a class in PHP use the "var" definition, and the data member is not typed until it is assigned a value. A data member may be an integer, an array, a union array (associative array), or even an object. method is defined in the class as a function, in the method to access data members, you must use $this->name, otherwise, the method is a function of the local variables.

Use new to create an object

The code is as follows Copy Code

$obj = new Something;

Then use the member function

The code is as follows Copy Code

$obj->setx (5);
$see = $obj->getx ();

The SetX member function assigns 5 to the member variable in the object (not the Class) obj, and then GetX returns the value 5.

You can also use object references to access member variables, for example: $obj->x=6; However, this is not a good method of object-oriented programming. I insist. You should use the member function to set the value of the member variable and to read the member variable through the member function. If you think that member variables are not accessible, in addition to using member functions, you will become a good object-oriented programmer. Unfortunately, PHP itself has no way of declaring that a variable is private, so it allows bad code to exist.

Inheritance in PHP is declared using extend.

The code is as follows Copy Code

class Another extends Something {
var $y;
Function Sety ($v) {
//Methods start in lowercase then use lowercase to seperate
//words in the method nam e example Getvalueofarea ()
$this->y= $v;
}

function GetY () {
return $this->y;
}
}

?>

Such objects such as "another" have member variables and method functions that are used by the parent class, plus their own member variables and member functions. Such as:

The code is as follows Copy Code

$obj 2=new another;
$obj 2->setx (6);
$obj 2->sety (7);

Multiple inheritance is not supported, so you cannot have a class inherit multiple classes.

You can redefine the method in the inheriting class, and if we redefine the GetX in "another", then we can no longer access the member function GetX in "Something". Similarly, if we declare a member variable with the same name as the parent class in the inheriting class, the variable that inherits the class will hide the variable with the same name as the parent class.

You can define a class's constructor, which is a member function that has the same name as the class, and is called when you create an object of the class.

The code is as follows Copy Code

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 objects in the following ways:

The code is as follows Copy Code

$obj =new Something (6);

The constructor automatically assigns a value of 5 to the member variable x, the constructor and member functions are normal PHP functions, so you can use the default parameters.

The code is as follows Copy Code

function Something ($x = "3", $y = "5")

And then:

The code is as follows Copy Code

$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 is defined in the same way as C + +, so you cannot pass a value to Y but let X take the default value, the pass of the argument is left to right, and the function uses the default parameter when there are no more arguments.

Only when the constructor of the inheriting class is called, the object of the inheriting class is created, and the constructor of the parent class is not called, which is the feature of PHP's different object-oriented languages, because the constructor call chain is an object-oriented programming feature. If you want to call the constructor of the base class, you have to explicitly call it in the constructor of the inheriting class. This allows it to work because the methods of the parent class in the inheriting class are all available.

The code is as follows Copy Code

function another () {
$this->y=5;
$this->something (); Explicit call to base class constructor.
}

?>

Polymorphism.

The code is as follows Copy Code

function nicedrawing ($x) {
Supose This is a method of the class Board.
$x->draw ();
}

$obj =new Circle (3,187);
$obj 2=new Rectangle (4,5);

$board->nicedrawing ($obj); Would call the draw method of Circle.
$board->nicedrawing ($obj 2); Would call the draw method of Rectangle.

?>

and encapsulation-related magic methods:

__set (): A method that is automatically called when a private member property value is set directly

__get (): method that is called automatically when a private member property value is obtained directly

__isset (); This method is called automatically when the private property in the object is isset to see if it is stored.

__unset (); Is the method that is called automatically when you delete a private property in an object directly unset

http://www.bkjia.com/PHPjc/372033.html www.bkjia.com true http://www.bkjia.com/PHPjc/372033.html techarticle Object-oriented design is a very important part of PHP program development, if you want to be a senior PHP programmer, we must know the specific usage and wording of object-oriented design. Maintenance Simple Mode ...

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