• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 875
  • Last Modified:

Why is get_called_class coming up as undefined??

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??
0
FairyBusiness
Asked:
FairyBusiness
  • 15
  • 8
  • 5
  • +1
1 Solution
 
Greg AlexanderLead DeveloperCommented:
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

0
 
FairyBusinessAuthor 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??
0
 
Greg AlexanderLead DeveloperCommented:
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

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
FairyBusinessAuthor 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()
0
 
Greg AlexanderLead DeveloperCommented:
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

0
 
FairyBusinessAuthor 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
0
 
Ray PaseurCommented:
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.
0
 
Greg AlexanderLead DeveloperCommented:
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

0
 
FairyBusinessAuthor 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. . .
0
 
FairyBusinessAuthor Commented:
I'm so frustrated nothings working.

I'm getting the message again though:

Fatal error: Call to undefined function get_called_class()
0
 
Greg AlexanderLead DeveloperCommented:
Yeah, if you could post the whole class, we could figure it out :)
0
 
FairyBusinessAuthor 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

0
 
FairyBusinessAuthor 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
0
 
Greg AlexanderLead DeveloperCommented:
So change

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

to

var $table = "users";
var $object;
0
 
Greg AlexanderLead DeveloperCommented:
Oh... there is not function called get_called_class() in your class
0
 
FairyBusinessAuthor 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

0
 
Ray PaseurCommented:
Perhaps you need to include the definition of the DBObject class?
0
 
Greg AlexanderLead DeveloperCommented:
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()?
0
 
FairyBusinessAuthor 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

0
 
Ray PaseurCommented:
Do you understand the concept of "constructors?"
0
 
FairyBusinessAuthor 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

0
 
Mohamed AbowardaSoftware EngineerCommented:
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
0
 
Ray PaseurCommented:
Yes, constructors are run automatically.  They are among the magic functions.  I did not see one in DBObject().
0
 
FairyBusinessAuthor Commented:
This is what my php info page says: PHP Version 5.2.17
0
 
FairyBusinessAuthor Commented:
I use $database globally
0
 
Mohamed AbowardaSoftware EngineerCommented:
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
0
 
Ray PaseurCommented:
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.
0
 
FairyBusinessAuthor 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??
0
 
Mohamed AbowardaSoftware EngineerCommented:
Contact the hosting company and ask them to update to the latest version of PHP, you might need to change the hosting company.
0
 
FairyBusinessAuthor 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??
0
 
Mohamed AbowardaSoftware EngineerCommented:
__CLASS__ will do exactly what you want without having to update PHP.

Use the following code instead:
private static function instantiate($record) {
		$class_name = __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

0
 
FairyBusinessAuthor Commented:
Thanks.  Can't wait for my server to be updated!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 15
  • 8
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now