1. Classes and objects
Object: An individual that actually exists in each of the objects in the class. $a =new User (); $ A after instantiation
Reference PHP aliases, two different variable names pointing to the same content
Encapsulation: Organizing the properties and methods of an object in a class (logical unit)
Inheritance: The purpose of code reuse is to create a new class based on the original class;
Polymorphic: A pointer to the parent class type is allowed to assign a pointer to the child class type.
-------------------------------------
2. Automatically load objects:
Automatic loading by defining special __autoload functions, this function is called automatically when references to classes that are not defined in the script are invoked.
1 [php] View plaincopyprint?
2 function __autoload ($class) {
3 require_once ("classes/$class. class.php");
6?
Why to use __autoload
1, first of all do not know where this class of files are stored,
2, the other one is not know when to use this file.
3, especially when the project file is very long, it is impossible to write the require in the beginning section of each file.
Instead of a
Require_once ("classes/books.class.php");
Require_once ("classes/employees.class.php");
Require_once ("classes/events.class.php");
Require_once ("classes/patrons.class.php");
Zend recommends one of the most popular ways to include a path in the file name. For example, the following example:
1 [php] View plaincopyprint?
2
3 View Sourceprint?
4//Main.class
5 function __autoload ($class _name) {
6 $path = Str_replace (' _ ', Directory_separator, $class _name);
7 require_once $path. PHP ';
8}
temp = new Main_super_class ();
All underscores are replaced with delimiters in the path, and the main/super/class.php file is removed from the previous example.
Disadvantages:
In the coding process, it is important to know exactly where the code file should be located,
And because the file path is hard-coded in the class name, if you need to modify the structure of the folder, we must manually modify all the class names.
Using the ' Include all ' method is convenient if you are in a development environment and are not very concerned about speed.
By placing all the class files in one or several specific folders, and then looking through the load.
For example
<?php
$arr = Array (
' Project/classes ',
' Project/classes/children ',
' Project/interfaces '
);
foreach ($arr as $dir) {
$dir _list = Opendir ($dir);
while ($file = Readdir ($dir _list)) {
$path = $dir. Directory_separator. $file;
if (In_array ($file, Array ('. ', '.. ')) | | Is_dir ($PATH))
Continue
if (Strpos ($file, ". class.php"))
Require_once $path;
}
}
?>
Another method is to create an associated configuration file between the class file and his location, for example:
View Sourceprint?
configuration.php
Array_of_associations = Array (
' Mainsuperclass ' = ' c:/main/super/class.php ',
' Mainpoorclass ' = ' c:/blablabla/gy.php '
);
The called file
<?php
Require ' autoload_generated.php ';
function __autoload ($className) {
Global $autoload _list;
Require_once $autoload _list[$className];
}
$x = new A ();
?>
3. Constructors and destructors
The PHP construction Method __construct () allows the constructor to be executed before instantiating a class.
The constructor method is a special method in the class. When you use the new operator to create an instance of a class, the constructor method is automatically called, and its name must be __construct ().
(Only one construction method can be declared in a class, but the constructor is called once every time the object is created, and the method cannot be invoked actively.
Therefore, it is often used to perform some useful initialization tasks. The method has no return value. )
Function: Initializes an object when it is used to create an object
The subclass performs the classification of the constructor Parent::__construct ().
destructor: __destruct () Definition: Special inner member function, no return type, no parameters, no arbitrary invocation, no overloading;
The resource allocated in the constructor is released automatically by the system when the class object is at the end of its life.
And the constructor method corresponds to the Destructor method, the destructor allows some operations to be performed before destroying a class or to perform some functions, such as closing a file, releasing a result set, and so on.
Destructors cannot have any arguments, and their names must be __destruct ().
PHP class and object full parsing method