Home Backend Development PHP Tutorial Three related problems encountered when using the Yii framework_PHP tutorial

Three related problems encountered when using the Yii framework_PHP tutorial

Jul 13, 2016 am 10:29 AM
yii Chengdu

The following are some of the experiences we gained when developing projects at Xinyi Network Company

Three problems encountered when using Yii framework

1. The problem of introducing global variables in the main.php file

Restore 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. During use, because the main.php file will be loaded in advance by Yii, some global operations are also placed in this file. There is no problem in loading some class operations. When a global variable is added once , and when using global to obtain global variables elsewhere, I found that no matter how hard I tried, I got NULL. After various attempts, I finally solved the problem by placing the imported location in the entry file index.php. What's the reason? Let’s reproduce Yii’s 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:

$global = array(1, 2, 3);
return array();

Place the two files in the same directory and run index.php directly. The output $global is NULL. If we directly output $global in the constructor of CApp, the result will be output. What's the reason? Scope issue!

When we define a variable in the main.php file, although we want to use it as a global variable, when we require it in the local scope, it only exists as a local scope variable. We mentioned in TIPI that function calls are nested. Each nesting will have a scope. The variables in this scope are only valid currently. When the nesting ends, the variable life cycle ends.

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

2. The class_exists problem when introducing third-party extensions

Yii framework Yii provides the automatic loading function of classes based on the autoload mechanism of PHP5. The automatic loader is the static method autoload() of the YiiBase class. When new is used to create an object or access a static member of a class in the program, PHP passes the class name to the class loader, and the class loader completes the include of the class file. But if we introduce a third-party extension, and the naming rules of the third-party extension are different from Yii's, we will often see an error saying that require XXX file failed. If you search "yii framework class_exists" in google, you will find that the role of Yii framework Xue Qiang has answered that users can use a method similar to: class_exists('MyClass', false).

The class_exists function checks whether the class has been defined. If the class pointed to by class_name has been defined, this function returns TRUE, otherwise it returns FALSE. In the PHP kernel, this function will search whether the class pointed to by class_name exists in the current class table. All classes will be converted to lowercase before searching, so they will not be case-sensitive. The second parameter refers to whether to use autoload. The default is to use it. At this time, the class_exists function will first execute autoload, and then check whether the class pointed to by class_name in the class table exists after executing autoload. So we can bypass autoloading by setting the second parameter to FALSE.

This may solve the problem, but what if we are using third-party code that cannot be modified? what to do? I simply hacked it myself and loaded the required classes before calling the third-party operation.

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

3. Yii error log

I won’t go into details about the problem. I just moved the configuration of the production environment to the development environment, so the error can no longer be seen. After adjusting the log rules, it was OK.

Yii’s processing of error logs relies on PHP’s set_error_handler function and set_exception_handler function. These two functions are handled in the initSystemHandlers method of CApplication.

This article is published by Xinyi Network, which specializes in website construction in Chengdu. For more information about yii, please pay attention to Xinyi Network’s subsequent releases. Xinyi Network’s official website is http://www.ir58.com

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/770157.htmlTechArticleThe following are some of the experiences we encountered when developing projects at Xinyi Network Company using the Yii framework Three questions 1. The problem of introducing global variables in the main.php file is restored...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Huawei teamed up with Chengdu High-tech to control TD Tech with 100% equity, but the 2.12 billion sale of Nokia failed to reach Huawei teamed up with Chengdu High-tech to control TD Tech with 100% equity, but the 2.12 billion sale of Nokia failed to reach Jan 19, 2024 pm 05:57 PM

According to news on January 19, the official website of the State Administration for Market Regulation announced the "acquisition of equity interests in TD Tech Co., Ltd. by Huawei Technologies Co., Ltd. and Chengdu High-tech Investment Group Co., Ltd.". This attention-grabbing dispute has a major update. progress. In this transaction, Huawei, Chengdu High-tech and other operators plan to jointly acquire 100% of the shares of TD Tech. Before this transaction, Huawei and Nokia Solutions and Networks GmbH & Co.KG (Nokia) indirectly held 100% of the shares of TD Tech and jointly controlled TD Tech. After the transaction, Huawei, Chengdu High-tech Investment Group Co., Ltd., Chengdu High-tech Jicui Technology Co., Ltd., Huagai Venture Capital Management (Beijing) Co., Ltd., etc.

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

'Jian Wang 3' new style 'Guangdu Town' visual effects upgrade 'Jian Wang 3' new style 'Guangdu Town' visual effects upgrade Apr 08, 2024 pm 09:52 PM

The Jincheng is full of spring flowers, the jade whip and gold are lightly dusted, and the popular main city of the Tang Dynasty, Guangdu Town, has been visually upgraded. It will be unveiled on the test server of the new expansion pack on April 9, and will be with everyone when the new expansion pack is launched. Meet in formal wear!! In recent years, more and more knights have stopped in Guangdu Town. The city has become unprecedentedly grand and busy, and Guangdu Town has become a famous and prosperous resort in the Tang Dynasty. The residents also earned a lot of money. After careful consideration, they decided to spend money to renovate Guangdu Town to make Chengdu more worthy of its reputation as "the first main city of the Tang Dynasty". The classic main city has been visually renovated, the regional layout remains the same, the NPC positions remain unchanged, the exquisite decoration has a new look, and the craftsmen's ingenuity can be seen in every detail! The tour guide is here today, come and take a first look at the new scenery of “Datang’s Popular CBD”! The sun shines on the head of Jincheng, and the light scatters on the flower building. The sun shines widely

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

Are there any experts? 'It's over!' I'm Surrounded by Beauties' prequel DLC (Chengdu chapter) revealed Are there any experts? 'It's over!' I'm Surrounded by Beauties' prequel DLC (Chengdu chapter) revealed Feb 20, 2024 am 08:46 AM

Following the release of the DLC "Heartbeat in the Room", "Done!" I'm surrounded by beautiful women! "The prequel DLC (Chengdu Chapter) has also recently exposed a promotional poster. The DLC will include 5 new heroines and is expected to start filming in Chengdu in early April 2024. The specific release time has not yet been determined. Based on the previous shooting time of the first DLC at the end of November 2023 and the release time on February 5, the prequel DLC may be released in June. However, "Heartbeat in the Room" only has 58% positive reviews on Steam, which is far lower than the 94% positive reviews of the main game. I don't know if the prequel DLC can save it.

See all articles