Jump to content


Photo

Problem creating objects dynamically

php namespace dynamic

Best Answer requinix, 06 December 2016 - 08:09 AM

$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. Go to the full post


  • Please log in to reply
3 replies to this topic

#1 JarlG

JarlG
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 December 2016 - 07:42 AM

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 good
Demo loaded ok
PHP Fatal error:  Uncaught Error: Class '\StreamComposer\Models\Product' not found in /var/www/streamcomposer.com/api/Models/Repositories/Repository.php:61
Stack 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 by JarlG, 06 December 2016 - 07:44 AM.


#2 requinix

requinix
  • Administrators
  • Confusing Administrator
  • 8,203 posts
  • LocationWA

Posted 06 December 2016 - 08:09 AM   Best Answer

$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 by requinix, 07 December 2016 - 03:02 AM.


#3 JarlG

JarlG
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 December 2016 - 08:23 AM

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!



#4 requinix

requinix
  • Administrators
  • Confusing Administrator
  • 8,203 posts
  • LocationWA

Posted 07 December 2016 - 03:03 AM

There's really no need for slow and expensive regular expressions here.
$modelName = substr($modelName, 0, -1);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users