Yii Framework Forum: How do I lazy load custom classes located in the components folder - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How do I lazy load custom classes located in the components folder Rate Topic: -----

#1 User is offline   SystemicPlural 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 132
  • Joined: 12-April 10

Posted 21 July 2010 - 07:32 AM

In particular, I have an abstract class that is extended by several sub classes. Each in their own file with the name of the file being the same as the name of the class.

At the moment, the only way I have worked out to access these classes is by including the following wherever I need them.
Yii::import('application.components.myClass.*');
require_once('myClass.php');


Can I lazy load these and access them through Yii::app()-> in some way?
I guess I need to put something in the config, in the components section. But I am not sure what.
0

#2 User is offline   yoshi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 28-February 09
  • Location:Germany

Posted 21 July 2010 - 07:55 AM

Hi,

you can add this path to the import-array in your config.

In main.php:
    'import'=>array(
        // ...
        'application.components.myClass.*',
    ),


and afterwards you can use something like
$myObject = new myClassXY();
and Yii will automatically search in application.components.myClass for a file/class named myClassXY and import it.

Regards
1

#3 User is offline   SystemicPlural 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 132
  • Joined: 12-April 10

Posted 21 July 2010 - 08:02 AM

View Postyoshi, on 21 July 2010 - 07:55 AM, said:

Hi,

you can add this path to the import-array in your config.

In main.php:
    'import'=>array(
        // ...
        'application.components.myClass.*',
    ),


and afterwards you can use something like
$myObject = new myClassXY();
and Yii will automatically search in application.components.myClass for a file/class named myClassXY and import it.

Regards


Thanks
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 21 July 2010 - 12:15 PM

View PostSystemicPlural, on 21 July 2010 - 07:32 AM, said:

Yii::import('application.components.myClass.*');
require_once('myClass.php');


Note that this will import the directory components/myClass/*. If you only want your classfile it's enough to use Yii::import('application.components.myClass');. But that's probably also obsolete as you already will have Yii::import('application.components.*'); in your config.

Quote

Can I lazy load these and access them through Yii::app()-> in some way?
I guess I need to put something in the config, in the components section. But I am not sure what.


Sure, you can configure custom application components. It's best to extend these from CApplicationComponent. Then configure it like this:

    'components'=>array(
        'somename'=>array(
            'class'=>'SomeClass',
            'somepublicvar'=>'configvalue',
        ),


It will be available with Yii::app()->somename and only be instantiated on first access of this property.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users