Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Namespace and variable class names


I think that is more a PHP-related question, but it interferes with Phalcon. :/

I'm building an abstract class for a controller with some built-in functions. This class has a function, where the a type is set (e.g. 'keywords'). The abstract class has a non-abstract function showAction ($id), where a generic detailed view is built up (for e.g. 'keywords', 'names', 'locations', ...). That meas, that the picked view is always the same. So the only thing that must be different is the called Model.

That model - you guessed it - is named like the type-variable, say 'Keywords'.

In only-PHP this should work: $unique = 'SlipBOX\Models\'.$type::findFirstById($id); (the controllers have as namespace SlipBOX\Controllers).

But the error message says: Fatal error: Class 'Keywords' not found in C:\xampp\htdocs\slipbox\app\controllers\UniqueAbstractController.php on line 49

I've tried also: $unique = '\SlipBOX\Models\'.$type::findFirstById($id);

I don't know, where is the error? Yes, it's a simple question, but I hope you help me!

Great thanks and best wishes Aljoscha


Try this: $namespace = '\SlipBOX\Models\'.$type; $unique = $namespace::findFirstById($id);

Hey Arius,

thanks for your answer! :) Changed the code to (need to use double backslashes, because of single quote signs and escape!): $type = ucfirst($this->_type); $class = '\SlipBOX\Models\'.$type; $unique = '\SlipBOX\Models\'.$class::findFirstById($id);

Now no error message occurs, but the view is empty!

When I hard code (change the code to) this: $unique = \SlipBOX\Models\Keywords::findFirstById($id);

View is loaded correctly with data.

So I've inserted an error-handling: if ($unique == FALSE) { $this->flashSession->error("LOAD_" . strtoupper($type)); return $this->response->redirect('uniques/list/' . strtolower($type)); }

But unique is not FALSE. :/


$type = ucfirst($this->_type); $class = '\SlipBOX\Models\'.$type; $unique = '\SlipBOX\Models\'.$class::findFirstById($id);

$unique = '\SlipBOX\Models\'.$class::findFirstById($id);

should be

$unique = $class::findFirstById($id);

Dammit, I'm so blind O:)

Thanks so much!