This article mainly introduces about the PHP object-oriented and instantiation of the object, has a certain reference value, now share to everyone, the need for friends can refer to
Class
Statement
[Modifier] class class name { [property] [Method]}
Precautions:
1) class name follows 大写
The Hump naming specification at the beginning
2) The opening and closing tags of curly braces must be in line.
Modifier
Private protected public var (not recommended)
Access rights
Instantiation of
Instantiate using the New keyword.
Brackets are not added.
Example
1. Declare class dog{ //2. Define Properties public $name = ' Wang Choi '; public $sex = null; 3. Define method public function bark () { echo ' bark ~ '; }} 4. Instantiate $d1 = new Dog ();
Instantiating an Object
Calls to properties, methods
Use to ->
access non-static properties | methods.
Example
Declare class dog{ //define Properties public $name = ' Wang Choi '; public $sex = null; Define method public function bark () { echo ' bark ~ '; }} Instantiate $d1 = new Dog ();//Use Properties and methods echo $d 1, Name;echo ' <br/> '; echo $d 1, bark ();
Internal call
Method can use a pseudo-variable when it is called inside the class definition $this
.
$this
, it represents the object being used.
Example:
Declare class dog{ //define Properties public $name = ' Wang Choi '; public $sex = null; Public Function Intruduce () { echo ' My name is '. $this->name; }}
Pass Assignment
1) Simple type , pass assignment to both sides, 相互独立
.
2) When a composite type is passed, the assigned value is the identifier of the object , so 变化一致
.
Simple Type examples:
$a = ' abc ';//pass Assignment $b = $a;//Change A's value $ A = ' QQ '; Var_dump ($a) ; Qqvar_dump ($b); Abc
Composite type Examples:
Class myclass{public $str = ' abc ';} $a is the identifier of the object $ A = new MyClass ();//pass assignment is equivalent to assigning the identifier to $b$b = $a; Var_dump ($a) ; Abcvar_dump ($b); abc//change the value of the A object str property $ A-str = ' 111 ';//$a and $b are identifiers var_dump ($a); 111var_dump ($b); 111//change the value of a itself $ A = 123;var_dump ($a); 123var_dump ($b); 111 Object Identifiers
Reference assignment
Whether it is a simple type or a type, the two sides are all in the same line of change.
Example:
$a = ' abc ';//reference Assignment $c = & $a; var_dump ($a) ; Qqvar_dump ($c); Qq