Ten: Type prompts:
Note that the type prompt function can only be used for prompts with parameters as objects, but cannot be used for prompts such as integers, strings, floating points, etc. Some methods require the incoming parameters to be the desired object type, and the following method can be used to enforce this alternative. To achieve the type prompt, just add the name of the existing class before the object type parameter of the method, such as: function funname(OtherClassName $otherclassINSName,$c....). Note that OtherClassName must be an existing class. as follows:
class em{ var $k=56; }
class test{
function __construct()
{ echo $this->addab(new em(),2); }
function addab(em $j,$c) //This method can be called internally or externally. As long as scope permits.
{ return $j->k+$c; }
}
$a = new test();
$b = new em();
echo $a->addab($b,2); //or $a->addab(new em(),2);
11. Class management:
1. instanceof keyword: used to analyze whether an object is an instance or subclass of a certain class or implements a specific interface: the following example, but be careful: the class name does not have any delimiter such as quotes, otherwise an error will occur. If test cannot be used 'test'
class test2{}
class test{}
class testChilern Extends test{}
$a = new test2();
$m = new test();
$i = ($m instanceof test);
if($i)echo '$m is an instance of the class test! <br />'; // get this value
switch ($a instanceof test){
case true :
echo 'YES<br />';
break;
case false :
echo 'No<br />'; //get this value
break;
}
$d=new testChilern();
if($d instanceof test)echo '$d is a subclass of class test! <br />'; // get this value
2. Determine whether the class exists: boolean class_exists(string class_name): class_exists('test');
3. Return the class name: string get_class(object), returns the class name of the instance when it succeeds, and returns FALSE if it fails:
$a = new test2(); echo get_class($a); //Return to test2
4. Understand the common attributes of the class: array get_class_vars('className'), return the key array: contains all defined public attribute names and their corresponding values. This function cannot be used as variables with instance names
5. Return class method: get_class_methods('test'); //or: get_class_methods($a); The instance name can be used as a parameter to return all non-private methods including the constructor.
6. print_r(get_declared_classes()) understands all class names in the current PHP version. There are 149 PHP5.
7. get_object_vars($a) returns an associative array of all common properties and their values in the instance. Note the difference between it and get_class_vars():
/* (1) get_object_vars($a) is used as the parameter with the instance name, while get_class_vars('test') is used as the parameter with the class name.
* (2) The attribute value obtained by get_object_vars($a) is the value after the instance is run, while the attribute value obtained by get_class_vars('test') is the initial definition in the class.
* (3) Both return an associative array and both return NULL values for unassigned attributes. If public $q is defined in the class test; then Array ( [v] => 5 [q]=>) is returned,
*/
8. Return the name of the parent class: get_parent_class($b);// or get_parent_class('test2'); Return the test
9. Determine whether the interface exists: boolean interface_exists($string interface[,boolean autoload])
10. Determine the object type: boolean is_a($obj,'className'), when $obj belongs to the CLASSNAME class, or its subclass, it returns TRUE, and if $obj has nothing to do with the class type, it returns FALSE. For example: is_a($a,'test')
11. Determine whether it is a child object of a certain class: When $b is inherited from the TEST class, return TRUE, otherwise FALSE. boolean is_subclass_of($b,'test');
12. Determine whether there is a method in a class or instance. method_exists($a,'getv') //or use method_exists('test','getv'), this function is suitable for non-public-defined scoped methods.
Examples of the above function:
class test{
public $v=2;
private $c=5;
function __construct(){
$this->v=5;
}
private function getv(){
return $this->v;
}
}
class test2 extends test{}
$a=new test();
$b=new test2();
print_r( get_class_methods('test')); //or: print_r( get_class_methods($a)); both return: Array ( [0] => __construct [1] => getv )
echo '<br />';
print_r( get_class_vars('test')); //Return: Array ( [v] => 2 ), different from the above, print_r( get_class_methods($a));
echo '<br />';
echo get_parent_class($b);// or get_parent_class('test2'); return to test
echo '<br />';
echo is_a($b,'test');// Return 1
echo '<br />';
if(is_subclass_of('test2','test'))echo ' is a subclass! '; //or (is_subclass_of($b,'test')), returns 1, and returns false when parameter 1 is $a,
echo '<br />';
echo method_exists($a,'getv') //or use method_exists('test','getv') to return 1. This function is also applicable to methods that define domains such as private.
11. Automatically load class library files:
When there are too many classes, for example, you need to load 3 class library files in one file:,, use three require_once('classes/);
require_once('classes/);
require_once('classes/);
It can be handled with PHP5's automatic loading function: In the global application configuration file, define a special function __autoload($class) function (__autoload is not a class method, it is just a separate function and has nothing to do with the class):
function __autoload($class){
require_once("classes/$class)
}
It doesn't matter where the function is placed. When creating a class instance, you don't have to call this autoload function. PHP will be done automatically. But it is important to note that the three types of "the class name used to create an instance on the calling page" must be the same as "the file name called", and "the name of the class in the file". In this way, there is no need to call __autoload(); if it is different, you must call __autoload('c'); and give it a file name prefix. like:
The code for the file is:
<?php
class c{
public $m=7;
}
?>The class name of the code here is c, and the file name is c.
Now to call:
<?php
function __autoload($class){
require_once "$";
}
$m = new c(); //The class that creates the instance call is also c
echo $m->m;
?>
At this time, PHP will automatically call class C in the root directory.
But if the code in it is:
<?php
class mm{
public $m=7;
}
?>
And the calling page code is:
<?php
function __autoload($class){
require_once "$";
}
# __autoload('c'); //If you do not add this line, you will get an error.
$m = new mm();
echo $m->m;
?>
An error will occur, prompting that the file cannot be found. At this time, you can add a line __autoload('c'); but this will not achieve the purpose of simplifying the code.
Family-class extensions of classes: Advanced features of classes:
1. Object cloning:
When an instance of an object is cloned, its attribute initial value inherits the current value of the cloned object.
class test
{
public $p=5;
function __clone(){ //Only work when cloning occurs. Used to change certain values when cloned
$this->p=15;
}
}
$a=new test();
echo $a->p;
$a->p=8; //If there is no effect of __clone() method, the P value of $b will be 8
$b = clone $a;
echo $b->p; //15
2. Object inheritance:
Classes that are not declared as final can be inherited, methods that are not defined by final and private can also be inherited, and attributes that are not defined by private can also be inherited. When a child class inherits the parent class or superclass, all allowed methods and properties of the parent class or superclass (grandfather class and grandfather's grandfather) can be used directly.
Key: Understand the characteristics of constructors and overloads in inheritance!
(I) Characteristics of constructors in inheritance:
1. When the parent class has a constructor but the subclass does not: the subclass will automatically execute the constructor of the parent class when instantiated. At this time, if you want to create an instance of the subclass, you need to introduce the required parameters in the parent class constructor, otherwise an error will occur. Even if the "subclass of subclasses" does not have a constructor, enter the required parameters for the constructor of the parent class of its parent class when creating an instance. PHP will search for the combined constructor from the subclass where the instance is located, and stop once it is found and use the constructor. It will not search upward again, so: if the subclass itself does not have a constructor, the one that is closest to the superclass and has a constructor shall prevail.
class cA{
public $name,$age;
function __construct($n) {
$this->name = $n;
$this->age = 25;
}
function __set($n,$v) {
$this->$n = $v;
}
function __get($n) {
return $this->$n;
}
}
class cB extends cA{
function funB1() { echo '<h3>Class cB execute success!</h3>'; }
}
class cC extends cB {
function funC1() { echo '<h3>Class cC FunC1!</h3>'; }
}
$b=new cB('Jack');
$b->name='John';
echo "$b->name : $b->age";
$b->funB1();
$c=new cC(); //An error will occur here. Since cB does not have a constructor, cA is the basis for upwards, and a parameter is required. Change to $c=new cC('David');
echo $c->name(); //David
2. When the subclass also has a constructor: At this time, regardless of whether the parent class has a constructor, the subclass's own constructor will be executed.
As above:
class cB extends cA{
function __construct() {
echo '<h3>this is Class cB \'s __construct!</h3>';
}
function funB1() {
echo '<h3>Class cB execute success!</h3>';
}
}
Now that class CB has its own constructor, the creation of instance $b=new cB('Jack'); parameter JACK will not work because the constructor of the parent class CA is not executed. Therefore, $b->name and $->age will not initialize the values. You need to assign additional values $b->name='Jack',$b->age=25;
If you want to execute the constructor of the parent class CA at this time, you can do this:
function __construct($n) {
parent::__construct($n); // Or: cA::__construct($n);
echo '<h3>this is Class cB \'s __construct!</h3>';
}
Since parent::__construct($n); will only search for the constructor of the parent class upwards, and stop and execute the currently found constructor as soon as it is found. Therefore, in the above example, if parent::__construct($n) is used in the last layer of class cC, and classes CB and CA have constructors, then the instance of cC will only execute the constructor of cB. cA will not be executed. At this time, if the CC instance wants to call the constructors of CA and CB, there are two methods:
A. Add parent::__construct($n) in CB
B. Change the constructor in CC to:
function __construct($n) {
cA::__construct($n); // That is: class name::constructor.
cB::__construct();
echo '<h3>this is Class cB \'s __construct!</h3>';
}
(2) Call the properties or methods of the parent class in the subclass:
1. Call the parent class method: Call the parent class method in the subclass, there are 3 methods:
$this->ParentFunction(); or
Parent class name::ParentFunction(); or
parent::parentFun();
2. Call the parent class attribute: only $this->ParentProperty;
(III) Overload:
In a subclass, you can define the same attribute or method as the parent class, and change the value or operation of the attribute or method of the parent class, which is called overloading. like:
calss ParClass{ function pfun(){ ....}}
class ChildrenClass extends ParClass{function pfun(){ ....}}} //Overload the pfun method of the parent class.
After overloading in a subclass, the newly defined methods or attributes you have overloaded are preferred.
You can also use parent::parentFun(); to call the parent class method in the subclass, but the obtained value is the parameter operation value entered by the subclass itself. Instead of the value that the method computes in the parent class.
3. Interface:
Interface: interface can be understood as a common specification of a set of functions. The greatest significance may be to specify a common method name for each development when multiple people collaborate.
Like abstract methods in abstract classes:
1. The specific implementation of the method cannot be defined in the interface. Instead, it is implemented by concrete classes (and non-abstract methods in abstract classes do not need to be defined anymore, only abstract methods and interfaces require implementation in concrete classes).
2. Like abstract classes, constants can be defined in the interface and directly inherited by concrete classes.
3. Concrete classes must implement all abstract methods of abstract classes (except non-abstract methods). Similarly, if a concrete class implements an interface through implements, all methods in the interface must be completed.
Interface implementation process: 1. Define the interface, 2. Use ..implement X, Y,... to connect with specific classes.
interface Info{ //Define the interface
const N=22;
public function getage();
public function getname();
}
class age implements Info //If you want multiple interfaces class age (extends emJob) implements Info,interB...
{
public $age=15;
public $name='Join';
function getage() {
echo "Grade is $this->age";
}
function getname() {
echo "The name is $this->name";
}
function getN(){
echo '<h3>The value of the constant N defined in the interface is: '.$this::N.' </h3>'; //Directly inherit the constant value in the interface.
}
}
$age=new age;
echo $age::N; //22, directly call the constant value in the interface.
$age->getN();
Regarding the difference between abstract classes and interface classes: When to use interfaces, when to use abstractions?
1. Relevance: When the created model is adopted by some closely related objects, use abstraction. For functions adopted by unrelated objects, use interfaces.
2. Multiple inheritance: PHP classes can inherit multiple interfaces, but cannot extend multiple abstract classes.
3. Public behavior implementation: Abstract classes can implement public methods in it, but interfaces cannot.
4. Namespace (PHP6)
Both the class library script and script have a class name class CNAME, and these two files must be called in the same file. This is where the namespace is used.
Buju:
1. Open the above two files A and B, and add a line to the first one:
namespace SPACEA; and namespace SPACEB; Customized name.
2. When instantiating a class, add a namespace and a double colon as a prefix in front of the class:
include '';
include '';
$a=new SPACEA::CNAME();
$b=new SPACEB::CNAME();
This way there will be no conflict.
But before PHP6 was officially released, this feature had not yet been decided.
5. Implement iterators and iterations.
See PHP Bible P142;
6. Use the Reflection API.
Simple example:
class a{ .... }
$c = new ReflectionClass('a'); //PHP built-in class.
echo '<pre>'.$c.'</pre>';
Output the structure and content of class a. See PHP Bible P145;