Registering automatic loading methods, serialization, and inheritance and rewriting

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.