<?php Tutorial
/* Declaration of Class
* 1. What are you going to develop and determine what kind of writing
* 2. Members of a class must belong to this class
* [modifier class's keyword] class class name {
* Member Properties:
* Member Method:
* }
* 3. When declaring a member property in a class, a modifier must precede it, and when you are unsure which word to use, use VAR or public
* A file saves only one class, and the filename contains the class name, File: class name. class.php
* The writing of the class name:
* Variable: AAABBBCCC
* Function: AAABBBCCC
* Constants: AAABBBCCC
* Class Name: AAABBBCCC
* 4. member properties in a class, if you create multiple objects, each object has a different property value, do not give the initial value, after the creation of a good object and then give values
*
*
* Instantiate objects by class
* 1. Using new to create an object, plus the class name, is the object that creates the class
* $ object refers to the =new class name;
* 2. As long as there is a new keyword is to create an object, create an object is in memory allocated a space
*
* Only objects have storage space in them
*
* The role of the object
*
* The allocation of objects in memory
*
* Use of objects
* Members of the object must be accessed through a reference to the object
* Object-> Member
*
* Object-> member properties
* Object-> member method
*
*
*
*/
---------------------------------------------------<?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.
-----------------------------------------------------
<?php
Class Another extends something {
var $y;
function Sety ($v) {
$this->y= $v;
}
function GetY () {
return $this->y;
}
}
?>---------------------------------------------------
Declaration of class (phone class)
Class phone{
declaring properties
var $pinPai;
var $color;
var $batteryCapacity;
var $screenSize;
Member Methods
function call () {
}
Function message () {
}
function Playmusic () {
}
function photo () {
}
}
Instantiation of a class
<?php
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 an object by:
$obj =new Something (6);
The constructor automatically assigns a value of 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")
Then:
$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 C + +, so you can't ignore the value of Y, and give X a default parameter, which is assigned from left to right, if
When the parameters passed in are less than the required parameters, they will use the default parameters.
When an object of a derived class is created, only its constructor is invoked, and the constructor of the parent class is not invoked, if you want to call the base
class, you must display the call in the constructor of the derived class. You can do this because the methods of all the parent classes in a derived class are
is available.
-----------------------------------------------------
<?php
Function Another () {
$this->y=5;
$this->something ();
//Show Call base class constructor
}
?>---------------------------------------------------
Class person{
var $name;
var $age;
var $sex;
function say () {
}
function Eat () {
}
function Run () {
}
//Instantiate
$p 1=new person;
$p 2=new person;
$p 3=new person;
<?php
$obj =new Classfoo ();
$str =serialize ($obj);
//Save $str to disk
//A few months later
//Mount str
$obj 2=unserialize ($str)
?> from disk-------------------------------- -------------------
You have restored member data, but do not include methods (according to the document). This causes the
member variables to be accessed only by using $obj2->x (you have no other way!). The only way, so don't try it at home.
//Access members of the object
$p 1->name= "Zhangsan";
Echo $p 1->name;