Loading and namespaces for the final keyword, class (file) of PHP

Source: Internet
Author: User
Tags php class

* * Final, Class (file) loading and namespaces

1. Final keyword-----End-state, final

The final keyword is used to decorate classes and methods, and decorated classes cannot be inherited, and decorated function methods (public) cannot be overloaded in subclasses.
If a method in the parent class is declared final, the child class cannot overwrite the method. If a class is declared final, it cannot be inherited.

Class uses the final keyword example:
Final class person{
......
}
When a final defined class is inherited, the following error is indicated:
Fatal Error:class Student may not inherit from final Class (person) in ...
Methods use the final keyword example:
Class person{
Final function say () {
......
}
}

2. Automatic loading of classes

PHP Class Auto-load
__autoload: It is written directly inside the method of the class.

The reason for the automatic loading of classes in PHP :

In the process of PHP development, if you want to introduce a class from the outside, you will typically use the include and require methods to include the file that defines the class. This is not a big problem when it comes to small-scale development. However, in large-scale development projects, doing so generates a large number of require or include method calls, which makes the code difficult to maintain without compromising efficiency, and the cost of require_once is great.
Automatically loaded updates for PHP classes are replaced by:
After PHP5, the Zend engine calls the __autoload function automatically when the PHP class is loaded, if the file is not included in the class, or if the class name is wrong. This function requires the user to implement the __autoload function themselves. After the PHP5.1.2 version, you can use the Spl_autoload_register function to customize the auto-load handler function. When this function is not called, the SPL custom spl_autoload function is used by default.
We can see that _autoload do at least three things, the first thing is to determine the class file name based on the class name, and the second thing is to determine the disk path where the class files are located (in our case the simplest case, the class is in the same folder as the PHP program file that called them), The third thing is to load the class from the disk file into the system. The third step is the simplest, only need to use Include/require. To implement the first step, the second step of the function, you must contract the class name and disk file mapping method at development time, so that we can find its corresponding disk file according to the class name.

3. Namespaces

in a broad sense, namespaces are a way of encapsulating things
** namespace can write aliases in order to simplify code
namespace is a container that is primarily designed to recognize the classes and functions under it. Once the namespace is defined, the following code belongs to the namespace, so the definition of the namespace is at the very beginning of the code.
for the same package, the code for the same namespace or sub-namespace is not necessarily defined in a PHP file, and the code under the sub-namespace is designed to do the work of a particular module. The combination is a full-package namespace.
If the code you write does not define a namespace, it means that it belongs to the global namespace (\ symbol), so you can refer to the class or function directly (without adding \ symbols).

In PHP, namespaces are used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:
1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.
The PHP namespace provides a way to group related classes, functions, and constants together.
Note: namespaces named php or PHP, and namespaces that begin with these names (for example, php\classes), are reserved for use as a language kernel and should not be used in user-space code.

Example:

Loading and namespaces for the final keyword, class (file) of PHP

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.