PHP Learning Note--php file introduction

Source: Internet
Author: User
Tags autoload autoloader php class php framework

in daily work development, my most commonly used file reference is include (include_once), and require (require_once), both of which have met the development needs of everyday simple CGI, but when in-depth understanding of the content of the PHP framework, You will find that these two keywords are far from enough, this article has collated some of the PHP file references involved in the knowledge.

Let's start with a brief introduction to the use of include and require

1.include

The included file is searched by the path given by the parameter, and if no directory (only the file name) is given, it is searched according to the directory specified by include_path (defined in PHP configuration file). If the file is not found under Include_path, the include is finally searched in the directory where the script file is called and in the current working directory.

2.require Statement

Require and include are almost exactly the same, in addition to handling failures differently. require generates an E_compile_error level error when an error occurs. In other words, it causes the script to abort, and the include only generates a warning (e_warning), and the script continues to run.

3.autoload mechanism

The __autoloader function is the default function to load the unknown PHP class, and when the unknown class appears in the PHP code, the program will automatically call the function, the function prototype and a simple implementation of the following

function __autoload ($classname) {    $filename$classname . ". PHP ";     include_once ($filename);}

You can usually implement your own __autoload () function in classes of different types , and you can use spl_ in SPL if you want to register your own __autoloade function Autoload_register (), the function can put a custom function into the autoloader queue, and when autoloader is triggered, the function in the queue will be called in turn.

Spl_autoload_register () prototypes are as follows

BOOL Spl_autoload_register ([Callable $autoload _function [, bool $throw = True [, bool $prepend = false]]])

Spl_autoload_register can be called multiple times, inserting multiple custom callback functions into the autoload function queue.

The __autoloader () function has a default implementation in the ZEND engine, which is spl_autoload (), when it is not willing to implement the __autoloader () function itself, You can use this PHP -loaded load function, which is usually used with the spl_autoload_extensions () function,spl_autoloader_extensions () You can specify the extension of the file in which the containing class resides, and thespl_autoload () function looks for the file in the include_path in the form of the class name + extension . You can then call the default spl_autoload () function in the code by calling Spl_autoload_register () with no parameters .

A. class.php

<? PHP class  function  write () {echo  "I am A";}}? >

b.php

<? phpspl_autoload_extensions (". class. php "); Spl_autoload_register (); $a=new  A ();   $a->write ();  // I am A?>

The purpose of the Spl_autoload_call () function is to allow us to manually invoke the callback function in the Spl_autoload_register () registration queue, which, in general , is only called when an unknown class is encountered autoloader the callback function in the queue,Spl_autoload_call can implement a manual call , calling the callback function in the queue until it is successfully loaded into the unknown class.

A. php

<?  class  a{}?>

B. php

<? PHP function A ($classecho "a";}; function B ($class) {echo "B";  Include($class. ". PHP ");}; Spl_autoload_register (a); Spl_autoload_register (B); Spl_autoload_call ("a")  ; // Output AB?>

4.use Keywords

The Use statement can refer to a class in another name space. the function of PHP name space is the same as other language namespaces, which is used to solve the conflicts of variables and functions in third-party classes.

a.php

<? phpnamespace TEST; class a{function  myfunc () {echo "This a::myfunc \ n";}}

b.php

<? PHP  Use test\a; require_once ("a.php"); class b{function  myfunc () {print "This b::myfunc \ n";}} $b=new  B (); $b-MyFunc (); $a=new  A; $a

Output:

This B::myfunc

This A::myfunc

PHP Learning Note--php file introduction

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.