Yii: requiring a .php file

試著忘記壹切 提交于 2019-12-23 07:31:38

问题


I develop a project with Yii.

I need to require a plain .php file (not component, not a class, just a regular sequence of PHP functions definitions). What is the correct way to do this under Yii framework? Should I use plain require_once()?

require_once(Yii::app()->basePath . '/extensions/my-php-file.php');

Right?


回答1:


Yes, that's correct.

Example:

require_once Yii::app()->basePath . '/extensions/PearMail/Mail-1.2.0/Mail.php';



回答2:


Method in your question is fine, but forces file location to be always in same folder.

Flexible Yii::import is intented for loading classes only, and only classes whose name is same as filename.

However if you want to use aliases to get path to file, you can still benefit from aliases by using getPathOfAlias, like that:

require_once Yii::getPathOfAlias('application.extensions.my-php-file') . '.php';

To include file relative to current file it is good to use dirname(__FILE__) or __DIR__ (php 5.3+)

require_once __DIR__ . '/some-file.php';


来源:https://stackoverflow.com/questions/17282136/yii-requiring-a-php-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!