The structure of a class: there can be three of things inside a class, that is, constants (constant), attributes, and methods, which can be understood as constants, variables, and functions outside the class.Copy CodeThe code is as follows:
<?php
Class TEST
{
Const NAME = ' value '; Constant
Public $name = ' value '; Property
Public Function name ()//method
{
echo ' value ';
}
}
?>
In this context, attributes and methods can be used to further differentiate the scope of properties and methods using public, protected, private three different keywords, properties and methods with the private keyword, and only methods in the class that are in it are called , properties and methods with the protected keyword, in addition to themselves, the methods in their own parent and subclass can also be called, and the properties and methods with the Public keyword can be called from an instantiated object, so the greatest benefit is to add some descriptive features to all properties and methods. , making it easier to organize and organize the structure of your code. The Const keyword is skipped first, and is spoken along with the static behind it.
The static keyword is different from the public, protected, private and another type of keyword (so it can be used with public, protected, private overlay):
Copy CodeThe code is as follows:
<?php
Class TEST
{
public static function name ()
{
echo ' value ';
}
}
?>
With the static keyword method, you can not instantiate the class directly through the "::" Symbol call, and public, protected, private collocation, you can also make the call to distinguish permissions, but generally with the public partner, The previously mentioned const keyword, which should be the public static type, can only be called by Self::name,test::name in such a way that constants, __construct,__destruct, and so on, All belong to the static.
The structure part of the class, the last two keywords are abstract and the Final,abstract keyword indicates that the class must be covered by his subclass, and the final keyword indicates that the class must not be covered by his subclass, and that the function of the two keywords is exactly the opposite, An abstract method is an abstraction, a class with abstract methods, and an abstract class, which is described later.
Use of the class:
There are two main ways to use a class, one is to use the New keyword, and the other is to use the "::" Symbol:
PHP code
Copy CodeThe code is as follows:
<?php
Class TEST
{
public static function name ()
{
echo ' value ';
}
}
Method 1: Use the New keyword
$test = new test;
$test->name ();
Method 2: Use the "::" Symbol
Test::name ();
?>
(1): Using the New keyword as an instantiation, the above $test is an object created by instantiating the test class, $test->name () is called the name method that invokes the $test object.
(2): When using a class with the New keyword, you can use $this to refer to the class itself.
(3): Use the "::" Symbol if the method must be with the static keyword, when using the New keyword, the method that is called must have the Public keyword (a method without public, protected, any one of the keys in private, The default is public)
(4): The same class can be instantiated by the new keyword into a number of different objects, but is isolated from each other; "::" When the symbol is used, the method is shared between multiple uses:
PHP code
Copy CodeThe code is as follows:
<?php
Class TEST1
{
Public $name = 0;
Public Function name ()
{
$this->name = $this->name + 1;
}
}
$test 1 = new TEST1;
$test 2 = new TEST1;
$test 1->name (); $name 1 = = 1
$test 2->name (); $name 1 = = 1
/*--------------------------------------------*/
Class TEST2
{
public static $name = 0;
public static function name ()
{
TEST2:: $name = TEST2:: $name + 1;
}
}
Test2::name (); $name = = 1
Test2::name (); $name = = 2
The relationship of the?> class:
The relationship between classes and classes is primarily abstract, interface, and inheritance:
PHP code
Copy CodeThe code is as follows:
<?php
Abstract class TEST1//Abstraction
{
Abstract public Function name1 ();
Public Function name2 ()
{
}
}
Class TEST2 extends TEST1 implements TEST3//inheritance
{
Public Function name1 ()
{
}
}
Interface TEST3//interface
{
Public function name2 ();
}
?>
(1) A class with the abstract keyword is an abstraction class, and the method with the abstract keyword is an abstraction, an abstract method in an abstract class that must be overwritten in a subclass.
(2) The class with the interface keyword, is the interface, the interface does not allow the implementation of any method, the interface of all methods, must be overridden in the subclass.
(3) The words with ClassA extends ClassB or ClassA implements ClassB are inheritance, extends means inheriting another class, implements means inheriting another interface, only extends one class at a time, However, multiple interfaces can be implements.
(4) Abstract classes, interfaces, and the methods that are ultimately inherited and implemented must all be public.
In the process of inheriting, the subclass will overwrite the parent class with the same method, and if you need to invoke the parent class's method in the subclass, you can use the parent keyword or the class name plus the "::" Symbol call:
PHP code
Copy CodeThe code is as follows:
<?php
Class TEST1 extends TEST2
{
Public Function name ()
{
Echo Parent::name2 ();
Echo Test2::name2 ();
}
}
Class TEST2
{
Public Function name2 ()
{
Echo ' value2 ';
}
}
$test = new TEST1;
$test->name ();
?>
Here again to explain the "::" method in the role of the class, a function is to invoke the constant without instantiation (actually also understood as static), static properties and methods, and then in the inside of the class, through the Self,parent and the class name to establish a convenient call channel.
The relationship between objects is mainly "= =" equals, "= = =" Equals, not equal to and clone:
PHP code
<?php
Class TEST
{
Public Function name ()
{
}
}
$test 1 = new TEST;
$test 2 = new TEST;
$test 3 = $test 1;
echo $test 1 = = $test 2? True:false; True
echo $test 1 = = $test 3? True:false; True
echo $test 2 = = $test 3? True:false; True
echo $test 1 = = = $test 2? True:false; False
echo $test 1 = = = $test 3? True:false; True
echo $test 2 = = = $test 3? True:false; False
?>
(1) Two classes as long as they have the same properties and methods, that is, "= =" equals.
(2) Two classes must be pointing to the same object to be "= = =" All equals.
Clone is very special, in the above example, $test 3 = $test 1 process is not to give $test 31 copies $test 1 objects, but to $test 3 point to $test 1, if you must obtain a copy of $test1, You must use the Clone keyword:
PHP code
Copy CodeThe code is as follows:
<?php
$test 3 = clone $test 1;
?>
Hooks for the class:
__autoload:
is a function name and the only hook used outside of the class, which is invoked when an object is instantiated without a pre-loaded class.
__construct
When the class is instantiated, the called hooks can do some initialization operations.
__destruct
The hook that is called when the class is destroyed.
__call
When an object attempts to invoke a nonexistent method, the called Hook
__sleep
This hook is called when the Serialize () function is used to sequence session operations on a class.
__wakeup
This hook is called when the Unserialize () function is used to deserialize a class.
__tostring
When an object is converted to a string, the hook is called (for example, Echo).
__set_state
This hook is called when the Var_export () function is called to manipulate a class
__clone
This hook is called when copying a class using the Clone keyword
__get
This hook is called when you get the value of a property in a class.
__set
When setting a property value in a class, this hook is called
__isset
When you use the Isset () function to determine the value of a property in a class, this hook is called
__unset
This hook is called when a property value is destroyed using the unset () function
Tips for classes:
In an instance of a class, you can use this form to pass parameters to the __construct hook:
PHP code
Copy CodeThe code is as follows:
<?php
Class TEST
{
Public function __construct ($para)
{
Echo $para;
}
}
$test = new Test (' value '); Show value
?>
The foreach () function can be used to iterate over the properties of a class or object, and when it is traversed, the public, protected, and private conditions are displayed:
PHP code
Copy CodeThe code is as follows:
<?php
Class TEST
{
Public $property 1 = ' value1 ';
Public $property 2 = ' value2 ';
Public $property 3 = ' value3 ';
Public Function name ()
{
foreach ($this as $key = $value)
{
print "$key = $value \ n";
}
}
}
$test = new test;
foreach ($test as $key = $value)
{
print "$key = $value \ n";
}
$test->name ();
?>
When passing parameters to a method in a class, arguments can be forced to determine that only arrays and objects are supported:
PHP code
Copy CodeThe code is as follows:
<?php
Class TEST1
{
Public Function name (TEST2 $para)
{
}
}
Class TEST2
{
}
$test 2 = new TEST2;
$test 1 = new TEST1;
$test 1->name (' value '); Error, because this parameter must be an object after the TEST2 instantiation
$test 1->name ($test 1); No error.
?>
Syntax compatible with PHP4:
PHP5 classes are PHP4 compatible, and these php4-era grammars are inherited, but are not recommended for use in PHP5 environments.
(1) using the var preset attribute is automatically converted to public.
(2) The class name is used as a constructor, and in the absence of a __construct constructor method, a function that is the same as the same as the class names is looked for as a constructor.
(turn) Study of class in PHP5