get_class_name |
[eluser]Simon 24[/eluser]
Hi there, Does anyone know how I can determine the classname in a function of My_Model when I call this function through an uninstantiated descendant class. Like so: Code: class My_Model extends CI_Model { I call the function from a controller like so: Code: Book::whoami(); The result of this is that the name of the controller gets printed. Does anyone know? Thanks! Sander
[eluser]gRoberts[/eluser]
Look at __class__ and other magic constants at http://php.net/manual/en/language.consta...efined.php
[eluser]Simon 24[/eluser]
Thanks for the reply, but I am a couple of steps further along. I'll try to clarify my problem with an example below. When using the same code in CodeIgniter as in a test file I get different results. So maybe there is something in the CodeIgniter Controller I can work around. Example with a plain test file, outside CodeIgniter: Code: class Crud { Results are: Code: Product::forwarded_call(); But when I try this code in CodeIgniter I get a different result: Product model: Code: class Crud extends CI_Model { Pages controller: Code: class Pages extends MY_Controller { Then I get these results: Code: Product::forwarded_call(); So in the 'forwarded call' I get the classname of the controller instead of the model. How can I fix this? Thanks! Sander
[eluser]gRoberts[/eluser]
Unfortunately, from what I understand, when using __FILE__ it is scope based. So if you call it within Crud, it will return Crud, but if inherit Crud with Product and then call a static function, the static function still belongs to Crud.
[eluser]PhilTem[/eluser]
I use this code in MY_Model which returns the name of the class that extends MY_Model Code: /** Maybe it helps you For non-instantiated classes you might need to replace $this with self, but dunno if this code snippet works with non-instantiated classes
[eluser]Simon 24[/eluser]
Thanks a lot guys. But I think I solved my problem otherwise. When I change the functions in de product model to static, I get the response I was looking for. The function get_called_class() then returns the name of the class I need. Code: class Crud extends CI_Model {
[eluser]CroNiX[/eluser]
Code: $this->router->fetch_method(); may also be useful. It might not work correctly though since you are using CI in ways the user guide states you shouldn't (multiple class definitions in single file). Personally, I'd look into libraries for a lot of what you're doing. |
Welcome Guest, Not a member yet? Register Sign In |