We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Why is get_called_class coming up as undefined??

FairyBusiness
on
Medium Priority
1,018 Views
Last Modified: 2012-05-11
Hi, I am trying to use get_called_class, but I keep getting an error:

Fatal error: Call to undefined function get_called_class()

private static function instantiate($record) {
	$class_name = get_called_class();
	$object = new $class_name; // Here is where it starts a new class for itself
	foreach($record as $attribute => $value) {
		if($object->has_attribute($attribute)) {
			$object->$attribute = $value;
		}
	}
	return $object;
}

Open in new window


What is wrong with the way I am using the function??
Comment
Watch Question

Greg AlexanderLead Developer

Commented:
If you are within a class, you gotta $this->


<?
private static function instantiate($record) {
        $class_name = $this->get_called_class();
        $object = new $class_name; // Here is where it starts a new class for itself
        foreach($record as $attribute => $value) {
                if($object->has_attribute($attribute)) {
                        $object->$attribute = $value;
                }
        }
        return $object;
}
?>

Open in new window

Author

Commented:
I changed it but I got this error:

Fatal error: Using $this when not in object context

I think maybe I cannot use $this-> when its static??
Greg AlexanderLead Developer

Commented:
Hmmm... don't have a editor and I can't remmeber... have you tried


<?
private static function instantiate($record) {
        $class_name = YouClassName::get_called_class();
        $object = new $class_name; // Here is where it starts a new class for itself
        foreach($record as $attribute => $value) {
                if($object->has_attribute($attribute)) {
                        $object->$attribute = $value;
                }
        }
        return $object;
}
?>

Open in new window

Author

Commented:
Well I used the name of the class that its in and I got this error message

Fatal error: Call to undefined method DBobject::get_called_class()
Greg AlexanderLead Developer

Commented:
if you dropped the private static does it work:

function instantiate($record) {
        $class_name = $this->get_called_class();
        $object = new $class_name; // Here is where it starts a new class for itself
        foreach($record as $attribute => $value) {
                if($object->has_attribute($attribute)) {
                        $object->$attribute = $value;
                }
        }
        return $object;
}

Open in new window

Author

Commented:
I think so, but I got a different error message this time:

Fatal error: Class 'Menu' not found in /hermes/web09c/b2950/moo.auroriellacom/index.php on line 10

I do not know why it would say this though. . . as I have it saved and included in my library
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Just curious - this looks like one of those things that you can do but you might not want to do.  Like multiple inheritance.  As Joel Spolsky says, "Just don't do that!"

What are you trying to make happen here?  If you can tell us that in plain language we may be able to offer a suggested design pattern that is easier to get working.
Greg AlexanderLead Developer

Commented:
Whatever is calling the instantiate function could pass the class_name to it:

<?
private static function instantiate($record,$class_name) {
        $object = new $class_name; // Here is where it starts a new class for itself
        foreach($record as $attribute => $value) {
                if($object->has_attribute($attribute)) {
                        $object->$attribute = $value;
                }
        }
        return $object;
}

//here is where you would call it
$this->instantiate($record,$this->get_called_class());
?>

Open in new window

Author

Commented:
I am trying to move some of my menu functions to a menu class.  In the tutorial that I was following he did a lot of static functions, although I still don't know when to make a function static or not.  And its giving me errors in the process.

But I don't know why its saying it cant find my menu class, thats really silly and should be something I should be able to get but its not happening. . .

Author

Commented:
I'm so frustrated nothings working.

I'm getting the message again though:

Fatal error: Call to undefined function get_called_class()
Greg AlexanderLead Developer

Commented:
Yeah, if you could post the whole class, we could figure it out :)

Author

Commented:
I went through and took away static to make all of the functions regular.  Ray is right, no need to make this harder then it has to be, especially when I don't get the static stuff yet.

So new error:

Notice: Undefined variable: table in /hermes/web09c/b2950/moo.auroriellacom/includes/DBobject.php on line 12
<?php
require_once 'includes/database.php';

class DBobject { 

// Common Databaes Methods

protected $table = "users";
private $object;

function find_all() {
	return $this->find_by_sql("SELECT * FROM " . $this->$table);
}
function find_by_id($id=0) {
	global $database;
	$result = $this->find_by_sql("SELECT * FROM  " . $this->$table . " WHERE id={$id} LIMIT 1");
	return !empty($result) ? array_shift($result) : false; //array_shift pulls the first item out
}
function find_by_sql($sql="") {
	global $database;
	$result = $database->query($sql);
	$object_array = array();
	while ($row = $database->fetch_array($result)) {
		$object_array[] = $this->instantiate($row);
	}
	return $object_array; // Must return an array!
}
private function instantiate($record) {
	$class_name = $this->get_called_class();
	$object = new $class_name; // Here is where it starts a new class for itself
	foreach($record as $attribute => $value) {
		if($object->has_attribute($attribute)) {
			$object->$attribute = $value;
		}
	}
	return $object;
}
private function has_attribute($attribute) {
	// get_object_vars returns an associative array with all attributes
	// (including private ones) as the keys and their current value as the value
	$object_vars = get_object_vars($this);
	// We don't care about the value, we just want to know if the key exists
	// Will return true or false
	return array_key_exists($attribute, $object_vars);
}

} // End class Database_Object

$db_object = new DBobject();

?>

Open in new window

Author

Commented:
Oh, I see why the table is undefined, I had a $ in front of it! I removed that and I am back to where I started in terms of errors  :)

Fatal error: Call to undefined method Menu::get_called_class() in /hermes/web09c/b2950/moo.auroriellacom/includes/DBobject.php on line 29
Greg AlexanderLead Developer

Commented:
So change

protected $table = "users";
private $object;

to

var $table = "users";
var $object;
Greg AlexanderLead Developer

Commented:
Oh... there is not function called get_called_class() in your class

Author

Commented:
Weird, after I changed them to var  I got a new error message:

Fatal error: Class 'DBobject' not found in /hermes/web09c/b2950/moo.auroriellacom/includes/menus.php on line 4

Here is my menu class
<?php
require_once 'includes/database.php';

class Menu extends DBobject { 

protected $table = "menus";
protected $menu = "nav";

public function get_nav() {
	global $database;
	$sql = $this->find_all();
	$sql .= "WHERE visible = 1 ";
	$sql .= "ORDER BY id DESC";
	$result .= $database->query($sql);
	$result .= $database->confirm_query($sql);
	return $result;
}

} // End class Database_Object
?>

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Perhaps you need to include the definition of the DBObject class?
Greg AlexanderLead Developer

Commented:
The original issue may have just been that there is not function get_called_class() .. do you have a function anywhere named get_called_class()?

Author

Commented:
I am trying to use this function:

http://us2.php.net/manual/en/function.get-called-class.php

Ok, here is my DBobject class:

<?php
require_once 'includes/database.php';

class DBobject { 

// Common Databaes Methods

var $table = "users";
var $object;

function find_all() {
	return $this->find_by_sql("SELECT * FROM " . $this->table);
}
function find_by_id($id=0) {
	global $database;
	$result = $this->find_by_sql("SELECT * FROM  " . $this->$table . " WHERE id={$id} LIMIT 1");
	return !empty($result) ? array_shift($result) : false; //array_shift pulls the first item out
}
function find_by_sql($sql="") {
	global $database;
	$result = $database->query($sql);
	$object_array = array();
	while ($row = $database->fetch_array($result)) {
		$object_array[] = $this->instantiate($row);
	}
	return $object_array; // Must return an array!
}
private function instantiate($record) {
	$class_name = $this->get_called_class();
	$object = new $class_name; // Here is where it starts a new class for itself
	foreach($record as $attribute => $value) {
		if($object->has_attribute($attribute)) {
			$object->$attribute = $value;
		}
	}
	return $object;
}
private function has_attribute($attribute) {
	// get_object_vars returns an associative array with all attributes
	// (including private ones) as the keys and their current value as the value
	$object_vars = get_object_vars($this);
	// We don't care about the value, we just want to know if the key exists
	// Will return true or false
	return array_key_exists($attribute, $object_vars);
}

} // End class Database_Object

$db_object = new DBobject();

?>

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Do you understand the concept of "constructors?"

Author

Commented:
I think so. They run automatically, right?  I have one in my database class
<?php
require_once 'includes/constants.php';

class MyDatabase { // Objects should usually be singular

private $conn; // Declares the variable
public $last_query; // It stores the last query ran

// Checks to see if magic quotes are turned on
// Checks to see if the user's version of php is 4.3.0 or higher
private $magic_quotes_active;
private $new_enough_php;

// Automatically runs the function because its a __construct
function __construct() {
	$this->open_conn(); // Opens the connection
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists("mysql_real_escape_string"); // PHP >= 4.30
}
// Opens the connection
public function open_conn() {
	$this->conn = mysql_connect(SERVER, USER, PASSWORD);
	if(!$this->conn) {
		die("Database connection failed: " . mysql_error());
	}
	else {
	$db = mysql_select_db(NAME, $this->conn);
	if (!$db) {
		die("Database selection failed: " . mysql_error());
	}
  }
}
// Checks the sql result
public function query($sql) {
	$this->last_query = $sql;
	$result = mysql_query($sql, $this->conn);
	$this->confirm_query($result);
	return $result;
}
// Checks the query
private function confirm_query($result) {
	if(!$result) {
		$output = "Database query failed: " . mysql_error() .
		"<br />";
		$output .= "Last SQL query: " . $this->last_query;
		die($output);
	}
}
// If the user has magic quotes turned on & php 4.3.0 or higher then use the mysql_real_escape_string function
// If not then addslashes to the value
public function clean_strings($value) {
	if($this->new_enough_php) { // PHP 4.3.0 or higher
		// undo any magic quote effects so mysql_real_escape_string can do the work
		if($this->magic_quotes_active) {
			$value = stripslashes($value);
			$value = mysql_real_escape_string($value);
		}
	}
	else { // before PHP 4.3.0
		// if magic quotes aren't already on then add slashes manually
		if(!$this->magic_quotes_active) {
			$value = addslashes($value);
			// if magic quotes are active, then the slashes already exist
		}
	}
	return $value;
}
// The array from the data in the database
public function fetch_array($result) {
	return mysql_fetch_array($result);
}
// Number of rows
public function num_rows($result) {
	return mysql_num_rows($result);
}
// Affected rows
public function affected_rows() {
	return mysql_affected_rows();
}
// Get the last id inserted over the current db connection
public function insert_id() {
	return mysql_insert_id($this->conn);
}
// Closes the connection
public function close_conn() {
	if(isset($this->conn)) {
		mysql_close($this->conn);
		unset($this->conn);
	}
  } 
} // End class MyDatabase

$database = new MyDatabase();
?>

Open in new window

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
What PHP version are you using?

get_called_class() is avaiable (PHP 5 >= 5.3.0)
http://php.net/manual/en/function.get-called-class.php
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Yes, constructors are run automatically.  They are among the magic functions.  I did not see one in DBObject().

Author

Commented:
This is what my php info page says: PHP Version 5.2.17

Author

Commented:
I use $database globally
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
This is not possible.

The concept of "called class" was introduced in PHP 5.3. This information was not tracked in previous versions.

You can use get_class() instead:
http://php.net/manual/en/function.get-class.php
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Upgrade to PHP 5.3.6.  You will need to do it anyway, as there are important security fixes.  And there is this on the PHP man page:

All PHP users should note that the PHP 5.2 series is NOT supported anymore. All users are strongly encouraged to upgrade to PHP 5.3.6.

Author

Commented:
How do I update it on my hosting site?? If I just download it on my computer will it affect my files on the internet too??
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Contact the hosting company and ask them to update to the latest version of PHP, you might need to change the hosting company.

Author

Commented:
eek, my hosting site said this:

We will upgrade the PHP version 5.3 in near future when we update our servers.

if I switch to a different hosting site will I have to redo my whole database??
Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks.  Can't wait for my server to be updated!!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.