First, the registration of automatic loading method and the implementation of serialization
Class common{
Public Function AutoLoad ($class _name) {
if (file_exists ('./'. $class _name. Class.php ')) {
Include ('./'. $class _name. Class.php ');
}else{
echo ' ERROR ';
}
}
public static function Autoload1 ($class _name) {
if (file_exists ('./'. $class _name. Class.php ')) {
Include ('./'. $class _name. Class.php ');
}else{
echo ' ERROR ';
}
}
}
Instantiating classes
$obj = new Common;
Registering the AutoLoad method as an automatic loading method
Spl_autoload_register (Array ($obj, ' autoload '));
Register Autoload1 static method for automatic loading method
Spl_autoload_register (Array (' Common ', ' autoload1 '));
$arr =array (' pass ' = ' admin ', ' dbname ' = ' bbs ');
$db = new MySQLdb ($arr);
echo ' <pre/> ';
Var_dump ($DB);
Serialization of data
$data = serialize ($DB);
File_put_contents ('./12.10txt ', $data);
Second, the implementation of deserialization
/* First, the class name of the deserialized object has changed because the class that the object belongs to is also required to be found when deserializing. If it is not found, then the object is deserialized into __php_incomplete_class, which is a predefined class of PHP!
So load class files
*/
Include './mysqldb.class.php ';
function __autoload ($class _name) {
if (file_exists ('./'. $class _name. Class.php ')) {
Include './'. $class _name. Class.php ';
}
}
$data = file_get_contents ('./12.10txt ');
Deserialization
$data = Unserialize ($data);
echo ' <pre/> ';
Var_dump ($data);
There is a MYSQLDB.class.php file, which is not posted here.
Iii. Inheritance and rewriting
Class goods{
Public $goods _name;
Public function __construct ($g _name) {
echo $this->goods_name = $g _name;
}
}
Inherited
Class Books extends goods{
Public $author _name;
Method overrides
Public function __construct ($g _name, $a _name) {
Calling the parent class's constructor method
Parent::__construct ($g _name);
echo $this->author_name = $a _name;
}
}
Instantiation of
$books = new Books (' Siege ', ' Qian Zhongshu ');
echo ' <pre/> ';
Var_dump ($books);
Registering automatic loading methods, serialization, and inheritance and rewriting