How do i create my class object in single line from a variable:
$strClassName = 'CMSUsers';
$strModelName = $strClassName.'Model';
$strModelObj = new $strModelName();
The above code successfully creates my CMSUsersModel class object but when i try:
$strClassName = 'CMSUsers';
$strModelObj = new $strClassName.'Model'();
it pops error.... saying:
Parse error: syntax error, unexpected '(' in
You can not use string concatenation while creating objects.
if you use
class aa{}
$str = 'a';
$a = new $str.'a'; // Fatal error : class a not found
class aa{}
$str = 'a';
$a = new $str.$str; // Fatal error : class a not found
So You should use
$strModelName = $strClassName.'Model';
$strModelObj = new $strModelName();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With