Three problems encountered in using the Yii framework

Source: Internet
Author: User

The following are some of the experiences we have gained when developing projects in Xinyi network.

Three problems encountered in using the Yii framework

1. Question about introducing global variables in the main. php file

To solve this problem, in the Yii framework, main. php is generally used as the configuration file of the entire Application, saving various parameters of the Application and directly returning the array. In the process of use, because main. the PHP file will be loaded by Yii in advance. Therefore, some global operations are also stored in this file, so there is no problem with loading some class operations. When a global variable is added, when I use global to obtain global variables elsewhere, I find that NULL is obtained no matter how hard I try. After a variety of attempts, we finally put the introduced location in the index. php file of the entrance file to solve the problem. Why? Let's reproduce the Yii main. php file loading. The following code

Index. php file:

Class CApp {
Public function _ construct ($ config ){
$ Config = require ($ config );
}
}

$ Path = "main. php ";
$ App = new CApp ($ path );

Global $ global;
Var_dump ($ global );

Main. php file:

<? Php
$ Global = array (1, 2, 3 );
Return array ();

The two files are placed in the same directory and run index. php directly. The output $ global is NULL. If $ global is directly output in the constructor of the CApp system, results are output. Why? Scope issues!

When we are in main. the PHP file defines a variable. Although we want to use it as a global variable, when we require in a local scope, it only exists as a local scope variable. We have mentioned in TIPI that function calls are nested. each nested function has a scope. Variables in this scope are valid only currently, nesting ends, and variable lifecycle ends.

Therefore, if we want to use the global variables in main. php as the global variables of the entire application, we need to use the require main. php file in the scope of the entry file.

2. class_exists when third-party extensions are introduced

Yii framework Yii provides automatic class loading based on the autoload mechanism of PHP5. The automatic loader is the static method autoload () of the YiiBase class (). When the program uses new to create objects or access static members of the class, PHP passes the class name to the class loader, and the class loader completes the include of the class file. However, if we introduce third-party extensions, and the naming rules of third-party extensions are different from those of Yii, we will often see an error saying that the require XXX file fails. If you search for "yii framework class_exists" in google, you will find that the role of the Yii framework Xue Qiang has answered that users can use methods similar to: class_exists ('myclass', false.

The class_exists function checks whether the class has been defined. If the class referred to by class_name has been defined, this function returns TRUE; otherwise, FALSE. In the PHP kernel, this function checks whether the class indicated by class_name exists in the current class table, and converts all the classes to lowercase before searching, so they are case-insensitive. The second parameter indicates whether to use autoload. It is used by default. In this case, the class_exists function runs autoload first, and then finds whether the class indicated by class_name exists after autoload is executed. Therefore, we can bypass automatic loading by setting the second parameter to FALSE.

This can solve the problem, but what if we use third-party code that cannot be modified? What should I do? I am a simple hack myself. When I call a third-party operation, I load the required class.

Later, another solution was adopted: directly use the second parameter of Yii: import to force the entire directory to be loaded.

3. Yii Error Log

The problem is not described in detail, but the production environment configuration is integrated into the development environment, so the error is invisible. After adjusting the log rules, it is OK.

Yii's processing of error logs depends on PHP's set_error_handler function and set_exception_handler function. In the initSystemHandlers method of CApplication, the two functions are processed.

This article is published by Xinyi network, which focuses on website construction in Chengdu,

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.