Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Why is get_called_class coming up as undefined??

Posted on 2011-04-22
32
Medium Priority
?
794 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??
0
Comment
Question by:FairyBusiness
  • 15
  • 8
  • 5
  • +1
32 Comments
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35450524
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
 

Author Comment

by:FairyBusiness
ID: 35450535
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
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35450543
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:FairyBusiness
ID: 35450550
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
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35450560
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
 

Author Comment

by:FairyBusiness
ID: 35450605
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35450623
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
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35450629
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
 

Author Comment

by:FairyBusiness
ID: 35450633
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
 

Author Comment

by:FairyBusiness
ID: 35450651
I'm so frustrated nothings working.

I'm getting the message again though:

Fatal error: Call to undefined function get_called_class()
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35450657
Yeah, if you could post the whole class, we could figure it out :)
0
 

Author Comment

by:FairyBusiness
ID: 35450681
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
 

Author Comment

by:FairyBusiness
ID: 35450692
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
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35450696
So change

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

to

var $table = "users";
var $object;
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35450705
Oh... there is not function called get_called_class() in your class
0
 

Author Comment

by:FairyBusiness
ID: 35450710
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35450744
Perhaps you need to include the definition of the DBObject class?
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35450750
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
 

Author Comment

by:FairyBusiness
ID: 35450757
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35450776
Do you understand the concept of "constructors?"
0
 

Author Comment

by:FairyBusiness
ID: 35450790
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
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35450851
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35450866
Yes, constructors are run automatically.  They are among the magic functions.  I did not see one in DBObject().
0
 

Author Comment

by:FairyBusiness
ID: 35450870
This is what my php info page says: PHP Version 5.2.17
0
 

Author Comment

by:FairyBusiness
ID: 35450873
I use $database globally
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35450906
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35450920
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
 

Author Comment

by:FairyBusiness
ID: 35450924
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
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35450931
Contact the hosting company and ask them to update to the latest version of PHP, you might need to change the hosting company.
0
 

Author Comment

by:FairyBusiness
ID: 35450965
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
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 2000 total points
ID: 35450969
__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
 

Author Closing Comment

by:FairyBusiness
ID: 35451003
Thanks.  Can't wait for my server to be updated!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question