-
Function: Automatically loads files of this class when the current page is using a class
- Parameter $classname the name of the class can be customized
-
*/
-
function __autoload ($className) {
-
$className =strtolower ($className);//preferably with this step, because the class is capitalized and some system-sensitive file names are cased
-
Require ("$className. class.php");//Load class file One.class.php Two.class.php in the same directory as this file
-
Echo '------------------------------
';//As a token at the time of the call to test
-
}
-
The following two classes are not defined in this file
-
$o =new One ();//Use the one class to initialize it
-
$t =new ();//Use the class of both to initialize it
-
?>
Copy Code
Note that if the function needs to load normally, the file name needs to be set to the class name (lowercase is recommended). Class.php class names and filenames have some kind of association, and if the function has an exception that cannot be caught by catch, fatal Error will be displayed.
2.__construct () constructor
The function names of constructors in C # and PHP4 are the same as the class names, and of course PHP5 is supported (if the __construct method is found to be the same constructor as the class name), the advantage of this is that if we want to change the name of the class, we just change the class name. Instead of changing the function name of the constructor and, to some extent, reducing the probability of our error, the sample code is simply written!
-
-
Class pcyoyo{
-
Private $url;
-
function __construct ($url) {
-
$this->url= $url;
-
}
-
}
-
?>
Copy Code
3.__destruct () destructor
__destruct destructor, PHP calls this method before the object is destroyed (that is, before it is purged from memory)
By default, PHP simply frees the memory occupied by the object's properties and destroys the object-related resources.
Destructors allow you to execute arbitrary code after an object is used to clear memory.
When PHP decides that your script is no longer related to the object, the destructor is called.
Within the namespace of a function, this occurs at the time of the function return.
For global variables, this happens at the end of the script. If you want to explicitly destroy an object, you can assign any other value to the variable that points to the object. You typically assign a variable to be on duty to null or call unset.
4.__call () Called when the called member method does not exist (or does not have permission) for the class, to do some action or to prompt the message after the error
-
-
Initialize a class
-
Class person{
-
Private $name;
-
Private $age;
-
Private $sex;
-
function Walk ($hour, $minute) {
-
echo "$hour: $minute We'll walk
";
-
}
-
function Run () {
-
}
-
Using the Magic Method __call
-
/*****
-
Function: Called automatically when an object calls a function that does not exist in the class, doing something
-
Parameters: $proName The called function name $args the parameter array $proName, $args the custom parameter can conform to its own style, but the meaning of the two parameters is fixed
-
*****/
-
function __call ($proName, $args) {
-
echo "The $proName doesn ' t exist! The parameters is ";
-
Print_r ($args);
-
Echo '
';
-
}
-
}//the end of class
-
Instantiate an Object
-
$p =new person ();
-
Calling methods that exist in the person class
-
$p->walk (5,39);
-
Calling a method that does not exist in the person class
-
$p->work ();//If the Magic method __call is not used, the program has an error, error message fatal Error:call to undefined method person::work () in d:/phpnow/htdocs/ holiday/magic__call.php on line 29
-
If the Magic method __call is used, __call is called automatically when an object calls a function that does not exist in the class, and is used to perform certain operations.
-
?>
Copy Code
5.__clone ()
This function is called automatically when an object is cloned, and it acts to initialize the cloned copy.
-
-
Initialize a class
-
Class computer{
-
//
-
Public $cpu = ' Intel ';
-
Private $monitor;
-
Private $keyboard;
-
Set Magic Method __clone () function
-
function __clone () {//If you do not set this magic method then the clone is exactly the same, the __clone function and the constructor of the class are similar to do some initialization operations
-
$this->cpu= ' AMD ';//$this refers to the newly created clone object
-
}
-
Set a destructor to compare the difference between = and clone
-
function __destruct () {
-
Echo ' *************************************
';
-
}
-
}//the end of the class
-
Instantiate an Object
-
$c 1=new computer ();
-
Use the referenced method to add a reference, because the object's reference is stored in memory in the stack memory, and if you press the pointer, then it is in memory that there is a pointer to the object that $c1 points to
-
We can tell by the destructor of the bar that this is not a copy of the object.
-
$c 2= $c 1;
-
Cloning an object using the Clone method
-
$c 3=clone $c 1;
-
Information comparison of the outputs
-
echo $c 1->cpu, '
';
-
echo $c 3->cpu, '
';
-
?>
Copy Code
6.__get () When the member property called by the object is not declared or the level is private or protected, we can perform some of our own operations in this function.
-
-
Class person{
-
Private $name;
-
Public $nation;
-
function __get ($para) {//must have one parameter
-
echo $para. ' Not present ';
-
}
-
}
-
$p =new person ();
-
$name = $p->name;//call the __get function
-
$nation = $p->nation;//never Call the __get function
-
?>
Copy Code
7.__set () Call this function when an assignment is made to an undeclared or level of private or protected, and we can do some of our own work in this function
-
-
Class pc{
-
Private $key;
-
function __set ($key, $value) {//must have two parameters one is a member property and the other is a value
-
echo ' pair '. $key. ' Assigned to '. $value. ' Failure ';
-
}
-
}
-
$p =new Pc ();
-
$p->key=123;
-
?>
Copy Code
1 2 Next last page
|