Welcome Guest, Not a member yet? Register   Sign In
get_class_name
#1

[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 {

  function whoami()
  {
    print get_called_class();
  }

}

class Book extends My_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
#2

[eluser]gRoberts[/eluser]
Look at __class__ and other magic constants at http://php.net/manual/en/language.consta...efined.php
#3

[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 {
public static function inhertited_call()
{
  print get_called_class();
  print __CLASS__;
}

public function forward()
{
  self::inhertited_call();
}
}

class Product extends Crud {
public function forwarded_call()
{
  self::forward();
}
}

class Pages {
function view()
{
  print "Product::forwarded_call();";
  print "<br />";
  Product::forwarded_call();
  print "<hr />";
  print "Product::inhertited_call();";
  print "<br />";
  Product::inhertited_call();
}
}

Pages::view();

Results are:
Code:
Product::forwarded_call();
ProductCrud
-------------------------------------------------
Product::inhertited_call();
ProductCrud

But when I try this code in CodeIgniter I get a different result:

Product model:
Code:
class Crud extends CI_Model {
public static function inhertited_call()
{
  print get_called_class();
  print __CLASS__;
}

public function forward()
{
  self::inhertited_call();
}
}

class Product extends Crud {
public function forwarded_call()
{
  self::forward();
}
}

Pages controller:
Code:
class Pages extends MY_Controller {
        public function test ()
{
  $this->load->model('Product');
  
  print "Product::forwarded_call();";
  print "<br />";
  Product::forwarded_call();
  print "<hr />";
  print "Product::inhertited_call();";
  print "<br />";
  Product::inhertited_call();
}
}

Then I get these results:
Code:
Product::forwarded_call();
PagesCrud
---------------------------------------------
Product::inhertited_call();
ProductCrud

So in the 'forwarded call' I get the classname of the controller instead of the model. How can I fix this?

Thanks!
Sander
#4

[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.
#5

[eluser]PhilTem[/eluser]
I use this code in MY_Model which returns the name of the class that extends MY_Model

Code:
/**
* Returns the name fhe instantiated class
*
*
* @access protected
*
* @param bool $ucfirst Whether to upper-case the first character
*
* @return string Returns the classes name
*/
protected function _class_name($ucfirst = FALSE)
{
    return ( $ucfirst ? ucfirst(get_class($this)) : get_class($this) );
}

Maybe it helps you Wink

For non-instantiated classes you might need to replace $this with self, but dunno if this code snippet works with non-instantiated classes Wink
#6

[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 {
public static function inhertited_call()
{
  print get_called_class();
}

public static function forward()
{
  self::inhertited_call();
}
}

class Product extends Crud {

//static is very important here! Otherwise I get controller classname
public static function forwarded_call()
{
  self::forward();
}
}
#7

[eluser]CroNiX[/eluser]
Code:
$this->router->fetch_method();
$this->router->fetch_class();
$this->router->fetch_directory();

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.




Theme © iAndrew 2016 - Forum software by © MyBB