dosgy00204 2011-12-27 20:38
浏览 46
已采纳

包括Yii中的外部库

How can I call these library functions from anywhere in my Yii app? I have a library:

#mylib.php

<?php
class MyLib {
    public function foo()
    {
        echo "hello!";
    }
}

and want to be able to call this function throughout my Yii app:

MyLib::foo();

I don't know where to place my library or how/where to import it. This is just an example of what I'm trying to do but I am trying to make a library that has multiple namespaces so I can access the library and have access to all the namespaces after importing it.

  • 写回答

4条回答 默认 最新

  • dongleiqiao2107 2011-12-29 09:31
    关注

    There are several ways.

    1. Register libraries' autoloader:

      // Enable Zend autoloader
      spl_autoload_unregister(array('YiiBase', 'autoload')); // Disable Yii autoloader
      Yii::import('site.common.lib.*'); // Add Zend library to include_path
      Yii::import('site.common.lib.Zend.Loader.Autoloader', true); // Require Zend autoloader
      spl_autoload_register(array('Zend_Loader_Autoloader', 'autoload')); // Register Zend autoloader
      spl_autoload_register(array('YiiBase', 'autoload')); // Register Yii autoloader
      
    2. Add library to the import section in your config/main.php:

      return array(           
          // Autoloading
          'import' => array(
              'application.lib.*',
              'application.components.*',
              'site.common.extentions.YiiMongoDbSuite.*',
          ),
      );
      
    3. Autoloading anywhere in your application:

      Yii::import('application.lib.*');
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。