object-oriented Programming in PHP: Ways to develop large PHP projects (i) (reprint)
Last Update:2017-02-28
Source: Internet
Author: User
Programming | object | Project PHP object-oriented Programming: Methods for developing large PHP projects (i)
Author: Luis Argerich translator: Limodou
This article describes object-oriented programming in PHP (Oop,object oriented programming). I will show you how to pass
uses some OOP concepts and PHP techniques to reduce coding and improve quality. 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;
}
}
?>--------------------------------------------------------------------------------
Transferred from Phpbuilder.com