Zend Framework uses Zend_loader components to dynamically load files and class usage _php instances

Source: Internet
Author: User
Tags readable zend zend framework

The example in this article describes how the Zend framework dynamically loads files and classes using the Zend_loader component. Share to everyone for your reference, specific as follows:

Loading files

The Zend_loader component enables you to load the file and determine whether the file is readable.

These two functions are implemented by the Zend_loader::loadfile () method and the Zend_loader::isreadable () method respectively.

Dynamic loading is the process of loading a file that can be referred to by a variable. When a file that needs to be loaded is a user input or a parameter of a method, it is difficult to load the file with the traditional loading method.

This process can be achieved by dynamic loading.

Example:

First build the loaded document temp.php, which only implements a simple output function, the code is as follows.

<?php
echo "Hello world!";
? >

Main program:

<?php
require_once ("zend/loader.php");
$filename = ' temp.php ';
Zend_loader::loadfile ($filename);

Execution results:

Hello world!

The description file has been successfully loaded in.

Determining file Properties

Syntax format:

Zend_loader::isreadable ($filename)

If the file is readable, the program returns True

The method is encapsulated by the PHP function is_readable (), but the two are still different. Is_readable () method

Files in the Include_path directory are not automatically found, while the Zend::isreadable () method is OK.

Example:

<?php
require_once ("zend/loader.php");
$filename = ' temp.php ';
if (zend_loader::isreadable ($filename)) {
  echo "file". $filename. " is readable! ";
} else{
  echo "file". $filename. " Not readable! ";
}
echo "<p/>";
if (is_readable (' zend/date.php ')) {
  echo "include_path file readable";
} else{
  echo "include_path file is unreadable";
}
echo "<p/>";
if (zend_loader::isreadable (' zend/date.php ')) {
  echo "include_path file readable";
} else{
  echo "include_path file is unreadable";
}

Results:

File temp.php is readable!
include_path files
can be read under unreadable include_path

This example shows that the is_readable () method cannot determine whether the Include_path file is readable, and the zend_loader::isreadable () method can be judged. Include_path file, refers to the file that is introduced in php.ini configuration.

Not only that, Zend_loader can also be loaded with classes. This will be explained below.

Dynamic Load Class

Zend_loader can not only dynamically load files, it can also be implemented through its loadclass () method.

Grammar:

Zend_loader::loadclass ($class, $dirs)

wherein, class is the specified class name, class is the specified name, and dirs is the path and filename of the file containing the class. Class names are based on the underscore corresponding to the corresponding directory

PHP files, such as Zend_controller_action, will point to zend/controller/action.php.

If the parameter $dirs is a string or an array, the method finds the corresponding directory in order and adds the first matching file. If the file does not exist, the method is also searched under the directory specified by Include_path.

Example:

<?php
require_once ("zend/loader.php");
Zend_loader::loadclass (' zend_date ');
$date = new Zend_date ();
Echo $date;

Results:

2013-3-18 05:30:16

Description, this code loads the required classes directly into the include_path. Then the instantiation, the output can be.

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

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.