object-oriented Programming in PHP: Ways to develop large PHP projects (i)
Source: Internet
Author: User
This article describes object-oriented programming in PHP (Oop,object oriented programming). I will show you how to pass
Use 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
Inherited
Polymorphic
The encapsulation is done in PHP by a class:
--------------------------------------------------------------------------------<?php
Class Something {
In an OOP class, the first character is usually uppercase
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
To be an integer, an array, an associated array (associative array), or an object.
method is defined as a function form in a class, you should use $this->name when accessing a class member variable in a method, otherwise the
method, it can only be a local variable.
To create an object using the new operator:
$obj =new something;
You can then 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.
It is not a good OOP habit that you can access data members in the same way as $obj->x=6 through a class reference. I strongly recommend that you pass
method to access a member variable. If you consider a member variable to be unhandled 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 easy to implement in PHP, just use the Extend keyword.
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