简体   繁体   中英

Error While instance a object from a String PHP

My problem is quite similar to this post: [a link] Creating PHP class instance with a string I want to instance an object from a String however my string came from a object property, I have this:

$type = strval($act->elementtype); $ty="Client";
$societe = new $type;

if I change $societe = new $ty it will work but no for $societe = new $type even when $type is equal to Client which is the name of my class. I recieve:

Fatal error: Class 'Client ' not found in....

It does't seem to be true that $type contains the same as $ty :

Fatal error: Class 'Client ' not found in....
                          ^

You can use var_dump() to trouble-shoot this kind of stuff.

Well.. If someone needs the answer sometime, the think was the type of my variable in my BD elementtype was type CHAR so I try to change it in posgresql and set it into CHAR VARYING and it works!! Hope it help! ^^

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