JarlG Posted December 6, 2016 Share Posted December 6, 2016 (edited) Hi, I'm trying to create objects on the run, but it I have run int a barrier that is really annoying:The code is: $tableName = $this->_stripNamespaceFromClassName(get_class($this)); $modelName = $tableName; $modelName[strlen($modelName) - 1] = ""; $demoModelName = "Product"; echo "-".$tableName."-\n"; echo "-".$modelName."-\n"; echo "-".$demoModelName."-\n"; $demoFullyQualifiedModelName = "\\StreamComposer\\Models\\$demoModelName"; $fullyQualifiedModelName = "\\StreamComposer\\Models\\$modelName"; echo "So far so good\n"; new $demoFullyQualifiedModelName(); echo "Demo loaded ok\n"; new $fullyQualifiedModelName(); echo "Fully loaded ok\n"; The result is (Echo string is bold):-Products--Product--Product-So far so goodDemo loaded okPHP Fatal error: Uncaught Error: Class '\StreamComposer\Models\Product' not found in /var/www/streamcomposer.com/api/Models/Repositories/Repository.php:61Stack trace:#0 /var/www/streamcomposer.com/api/Command/setupPayment.php(27): StreamComposer\Models\Repositories\Repository->loadAll()#1 /var/www/streamcomposer.com/api/Command/setupPayment.php(32): StreamComposer\Command\SetupPayment->createPlans()#2 {main} thrown in /var/www/streamcomposer.com/api/Models/Repositories/Repository.php on line 61 As you see the $demo.. object loads fine, but the $fully.. does not :-( Thanks in advance Edited December 6, 2016 by JarlG Quote Link to comment https://forums.phpfreaks.com/topic/302688-problem-creating-objects-dynamically/ Share on other sites More sharing options...
Solution requinix Posted December 6, 2016 Solution Share Posted December 6, 2016 (edited) $modelName[strlen($modelName) - 1] = "";Don't do that. You cannot use [] to remove characters - only replace. What the code above actually does is replace with a \0. I don't know what character you're trying to remove but I suggest looking at rtrim. [edit] The character is 's'. Which should be obvious given the code and output above. Eh. Edited December 7, 2016 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/302688-problem-creating-objects-dynamically/#findComment-1540061 Share on other sites More sharing options...
JarlG Posted December 6, 2016 Author Share Posted December 6, 2016 Thanks. Yes I just found out that that was the case :-) I used: $modelName = preg_replace('/.$/', '', $modelName); instead which also worked. So thanks, and now I got an explanation for why and not just a solution. Much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/302688-problem-creating-objects-dynamically/#findComment-1540062 Share on other sites More sharing options...
requinix Posted December 7, 2016 Share Posted December 7, 2016 There's really no need for slow and expensive regular expressions here. $modelName = substr($modelName, 0, -1); Quote Link to comment https://forums.phpfreaks.com/topic/302688-problem-creating-objects-dynamically/#findComment-1540092 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.