One: Structure and invocation (instantiation):
Class classname{}, called: $obj = new ClassName (), and when the class has a constructor, it should also pass in the parameter. such as $obj = new ClassName ($v, $v 2 ...);
two: Constructors and destructors :
1. Constructors are used for initialization: Use __construct (), with parameters.
2, but the destructor cannot take parameters (used to perform some operations or functions before the pin goes to a class). Destructors are named with __destruct (). At the end of the script execution, PHP will pin out the objects in memory, so it is not necessary to analyze the functions, but some, such as cookies, should be used to pin off the function.
Knowledge Points: Constructors are also provided in PHP4, but use a class method with the same name as the class, which is still compatible with PHP5, and when a class does not contain __construct, it looks for a method with the same name as the class, and if found, it is considered a constructor, as follows:
Class Test
{var $b;
function test () {$this->b=5;}
function Addab ($c) {return $this->b+ $c;}
}
$a = new test (); Echo $a->addab (4); Returns 9
3. PHP does not automatically call the parent class's constructor (which does not support constructor overloading) and must be explicitly called using the parent keyword.
Class employee{
function __construct () ....
}
Class Manager Extents employee{
function __construct () {
Parent::_construct ();
Echo ' The parent constructor of this subclass is called! ';
}
}
Of course, you can also call the constructors of other classes that do not have any relation to the instance. Just precede the __construct () with the class name. Such as:
Otherclassname::__construct ();
Main family members of a class: properties, methods, constants, static members
the properties of the class:
There are two ways to assign or value the properties of a class.
1. Use public scopes to common keywords.
2, using __set () and __get () to assign values and values, the former is called the Set method (setter) or modify method (Mutator), the latter is called the Access method (accessor) or Get method (getter). It is recommended to use this method: advantages:
A, data validation can be performed uniformly in __set ().
B, to facilitate the unified management of properties.
Attention:
First: __set () and __get () only work on private properties, and for properties defined with public, they are two lazy, as follows:
Class test{
Protected $a =9, $b =2, $c;
Public $d;
function __set ($n, $v) {$this, $n = $v +2;}
function __get ($name) {return $this, $name +2;}
}
$a = new test ();
$a->b = 5; echo "
"; Echo $a->b;
The instance is only for $ A, $b, $c settings are filtered and returned by __set and __get, and for $d, it will not work. such as $a->d=5, return or 5.
Second: __set ($n, $v) takes two parameters. __get ($n) can have only one parameter. Instance:
Class test{
Private $a =5, $b =6, $c;
function __set ($n, $v)
{
if ($n = = ' A ' && $n >0)
$this $n = $v;
Else
$this $n = $v +2;
}
function __get ($name)
{
return $this $name; If you change to return $this $name + $this->addab (); If the value of a is called, the value of A+a+b is actually returned. The default is 5+5+6=16.
}
function Addab ()
{return $this->a + $this->b;}
}
$e =new test ();
$e->a = 11; Note: The internal use of the class $this-> $n that is, the variable, but the external instance to use $e->a way.
$e->b = 12; Get 14
$e->k = 22;
The properties of the class can be freely extended, as in the above example K, whether or not with __set, when an instance is established, you can use $e->newproperty = XX; directly to create a property, but it is not recommended.
Iv. methods of the class:
Understand the functions in a class.
Call:
1, internal call: can use $this->fanname (), or $this->addab () or Test::addab ();
2. When instantiating the call, use $e->addab (); For the $this keyword is not used in this method, as in the previous example:
function Addab () {return $this->a+ $this->b;}
Instead: function Addab () {return 25;} That method is called in the external instance, also available "$e:: Addab ();" or "Test::addab ();"
class constants:
If the properties of a class are understood as variables in a class, then the constants and variables of the class are different, and are defined by:
class test{
private $a;
Const PI = ' 3.14 ';
.....
//Call the above constants in a class in two ways, "$this::P i", or "class name::P I", here is the test::P I, as follows:
function GetValue () {
return $this->a * $this ::P i; or $this->a * Test::P I, use the This keyword or class name, but both use a double colon.
}
}
$e = new test ();
$e->pi = 5; Note that here, just create a property called Pi, instead of changing the value of the PI constant in the class.
Echo $e::P i;//This is the constant that invokes the class. The
constant can only be called with double colons::. And you cannot change its value.
calling a class constant after an external instantiation of a class also has two methods. The method is
$e::P i or "Test::P I", in common is to use a colon, the difference is that the external cannot use the This keyword, only with the instance name, but the class name::P i is generic.
Vi. Static members of a class (static properties or Static methods):
If you need to create a field or method that is shared by instances of all classes. You have to use static members. There are two characteristics:
1, a static member is a communist, it makes all instances of the class on the script call, but not with the use of the class's specific instance name, but outside of the class, unified use "Class Name:: $ member name" method called. The inside of the class is invoked uniformly using the "self::$ member name".
2, when each new instance is created, the static member recalculates from the last value of the last instance created, rather than starting with the initial value in the class.
3, for a static member defined with public, you can change its value externally. Private and so on are not.
Class test{
public static $v = 0,
function __construct () {self:: $v + +;}
static function Getv () {return self:: $v;}
}
$a = new test ();
Echo Test::getv (); Returns 1
$b = new test ();
Echo Test::getv (); Returns 2
Test:: $v = 8;//Change the value of a static member because of a member defined by public.
$c = new Test ();
Echo Test::getv ();//Return 9
Seven, the key words:
(i) this keyword: used for the internal reference class of the class itself. To access a property or method or constant, such as a $this-> property name or method name. $this:: Constant name. This can also be used in subclasses of the class to refer to a property or method of its own.
(ii) Double colon "::" Keyword: used to invoke constants, static members.
(c) The Self keyword: calls a static member inside a class with a double colon mate, such as self:: $staticVar. Inside a class, you cannot invoke a static member with $this.
(iv) __tostring (): Use __tostring () in a class to turn the class into a string and print the class, which is of little use: for example:
Class test{public $p;
Public Function __tostring () {return Var_export ($this, TRUE);}
}
$a =new test ();
echo $a; Output: Test::__set_state (Array (' P ' = NULL,)), or written as: Echo $a->__tostring ();
(v) __clone (): This keyword does not function when cloning an object, and is used to change certain values when cloning.
(vi) __call (): Method overload, reference the following example:
Class cb{
function __call ($method, $n) {
if ($method = = ' Showvartype ') {
if (is_numeric ($n [0])) {//cannot be used with $n. To use $n[0];
$this->displaynum ();
}else if (Is_array ($n [0])) {
$this->displayarr ();
}else{
$this->displayother ();
}
}
}
function Displaynum () {
Echo '
This is a number!
';
}
function Displayarr () {
Echo '
This is an array!
';
}
function Displayother () {
Echo '
Not an array or a number!
';
}
}
$x = ' a ';
$y =array (' A ', ' B ');
$b =new CB;
$b->showvartype ($x); is not an array or a number
$b->showvartype ($y); This is an array
Note that the Showvartype () method cannot be defined in the class, otherwise the code cannot be used.
(vii) Extends: Inheritance: Class a{} Class B extends a{} classes B inherits classes a
Attached: Memory: Later unified in the call method or property with "," and call the constant with a double colon "::", will not be confused.
Viii. scope of methods and properties:
There are 6 kinds: public (default, can be omitted, also equivalent to PHP6 var declaration), Private (private, also cannot be used by subclasses), protected (private, but can be used by subclasses), abstract (abstract, reference), final (block in subclasses- Also known as overloading, block inherited, used to decorate class names and methods, such as final class test{final function fun () {}}, but not for properties), static (static)
IX: Abstract classes and abstract methods (abstract--Note: There is no so-called abstract attribute):
Abstraction can be understood as a parent class that defines a template or base class for a subclass. Scope Abstract is declared only in the parent class, but is implemented in subclasses. Precautions:
1, abstract class can not be instantiated, only quilt class (concrete Class) After the implementation of the inheritance.
2. An abstract class must implement all the abstract methods of the abstract class in its subclasses. Otherwise, there will be an error.
3, in the abstract method, just declare, but can not be specifically implemented: such as abstract function Gettow () {return $this->p;} Is wrong, can only declare this method: Abstract function Gettow (); (not even square brackets {}), abstract methods and abstract classes are mainly used in complex class hierarchy relationships. This hierarchical relationship needs to ensure that each subclass contains and overloads some specific methods. This can also be achieved through the interface
4. Attribute cannot be named abstract attribute, such as abstract $p = 5 is wrong.
5. Only a class that is declared abstract can declare an abstract method, but if the method is declared abstract, it cannot be implemented specifically. Such as:
Abstract class Employee
{
Abstract function A (...);
Abstract function B (...);
}
Later on, the parent class is extended to form various subclasses (such as managers, employees, cashiers).
6. In an abstract class, if you want to implement a specific method, you cannot declare it abstract. This may actually be more meaningful. You can extract the common parts of several class libraries into an abstract class, and other classes inherit the abstract class. As follows:
Abstract class baseshop{
Const tax=0.06; Defining constants in abstract classes
Public function Buy ($gid) {//If it is defined as an abstract method, abstract function buy () cannot implement the principal here.
Echo (' You have purchased the ID: '. $gid. ' goods ');
}
Public function Sell ($gid) {
Echo (' You have sold the ID: '. $gid. ' goods ');
}
Public Function View ($gid) {
Echo (' You have viewed the item ID: '. $gid. ');
}
}
Class Ballshop extends baseshop{
var $itme _id = null;
Public Function __construct ()
{
$this->itme_id = 2314;
}
Public Function open ()
{
$this->sell ($this->itme_id);
}
Public Function Gettax ()
{
echo printf ('
The average tax rate is%d%%.
', $this:: tax*100);
}
}
$s = new Ballshop;
$s->open (); You sold a product with an ID of: 2314
$shop->gettax ();
http://www.bkjia.com/PHPjc/321371.html www.bkjia.com true http://www.bkjia.com/PHPjc/321371.html techarticle One: Structure and invocation (instantiation): Class classname{}, called: $obj = new ClassName (); When a class has a constructor, it should also pass in the parameter. such as $obj = new ClassName ($v, $v 2 ...); Two: Construction ...