简体   繁体   中英

In CodeIgniter 3, Can I create a new Class instance using a variable?

This code works...

$season_view_builder =  new Hockey_season_view_builder($season_id);

However, if I use the following code I get the white screen of death.

$builder = 'Hockey_season_view_builder';
$season_view_builder =  new $builder($season_id);

Can CodeIgniter create new class instances from a variable name? Is there some kind of weird codeigniter syntax to this?

I really need to generate and return new classes dynamically to make my factory pattern work.

Thanks for any help! I'll keep trying different approaches and post if I find anything.

I was looking at another post and saw the correct answer which had to do with using namespaces. I've kind of hacked my CodeIgniter to use namespaces so I needed to do

$builder = 'Hockey_season_view_builder';
$var = __NAMESPACE__ . '\\' . $builder;
$season_view_builder =  new $var($season_id);

Thank you and sorry about the foolish question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM