2Answers
Answer

Yes, that's correct.

Example:

require_once Yii::app()->basePath . '/extensions/PearMail/Mail-1.2.0/Mail.php';
  • 10
Reply Report
      • 1
    • Require is not a function, no need for (): This is right: require_once Yii::app()->basePath . '/extensions/PearMail/Mail-1.2.0/Mail.php';
      • 1
    • @lin: I know it is a statement and not a function. Your version certainly looks more pleasing, but I don't think it matters which one you use (except for readability, maybe).
      • 2
    • Yii::$app is the static var for the Yii2 Application class, 'yiiwebApplication'. i tried this and it worked for me require_once Yii::$app->basePath . '/extensions/PearMail/Mail-1.2.0/Mail.php';

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';
  • 6
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions