Call to undefined method stdClass::

Whubbathwhatnow?

I’m learning OOP and am still quite green, but how is this not defined?

I keep receiving this error

Fatal error: Call to undefined method stdClass::profile_photo() in...
<?php
require_once('_config.php');

class Models {
	protected static $table_name = 'models';
	protected static $db_fields  = array('id','name','profile_photo');
	public $id;
	public $name;
	public $profile_photo;
	
	public static function findbyid($id) {
		global $db;
		if($object = $db->get_row("SELECT id,name,profile_photo FROM models WHERE id=".$id)) {
			return $object;
		} else {
			return false;
		}
	}
	
	public function profile_photo() {
		return MEDIA.'/'.$this->name.'/photos/'.$this->profile_photo;
	}
}

$model = Models::findbyid(1);
echo $model->profile_photo();

?>

findbyid is obviously not returning a Models object thus no profile_photo method.

It is returning something. I’m using ez_sql, and by default it returns objects instead of arrays. I do get a result and can print the id, etc. If I change findbyid() to a static method, then do

$model = Model::findbyid(1);
echo $model->profile_photo;

the profile photo prints!

I don’t know whats going on but the code siniplet you provided says, findbyid is not returning an object of Model. But what ever “$db->get_row” is returning is not a class of Model.

Here’s what I get when I run $db->vardump($object). As you can see, an object is returned. My head is smoking…

stdClass Object
(
[id] => 1
[name] => Amelia
[profile_photo] => YwRglHbd
)

Type: Object
Last Query [1]: SELECT * FROM models WHERE id=1
Last Function Call: $db->query(“SELECT * FROM models WHERE id=1”)
Last Rows Returned: 1

echo $model->profile_photo;

profile_photo is a property not a method.


$model->profile_photo();

That is a method call, incorrect unless a method was added using closure, but that is another topic.

I was just showing that something was being returned. :slight_smile:

Well, I’m totally lost. I took a course on OOP and calling methods was something I was taught. This was an example

class Person {

	public function say_hello() {
		echo 'hello';
	}
}

$person = new Person();
$person->say_hello();

Are you saying this is incorrect? I’m not sure what I should be doing at this point…

Returning an object of stdClass not an object of Model. (The name of the class in your snipplet is named Model) Thus when you try and call a method that only exists within the Model object it fails.

From the documentation I am not seeing any direct method to change the mapping to a custom class, looks like its either an array or stdClass. If you want to create a new instance of model you would need to do that manually. Something like this is how I would recommend doing it using a proper mapper pattern.

(untested)


class ModelMapper {

	protected
	
	$_db;

	public function __construct($db) {
		$this->_db = $db;
	}

	public function findById($id) {
	
		$row = $db->get_row('SELECT id,name,profile_photo FROM models WHERE id='.$id);
		return $row?$this->_map($row):null;
	
	}
	
	protected function _map($row) {
		
		$model = new Model();
		$model->id = $row->id;
		$model->name = $row->name;
		$model->profile_photo = $row->profile_photo;
		return $model;
		
	}

}

class Model {
	public
	$id
	,$name
	,profile_photo;
	
	public function getProfilePhotoPath() {
		return MEDIA.'/'.$this->name.'/photos/'.$this->profile_photo;
	}
	
}

$mapper = new ModelMapper($db);
$model = $mapper->findById(45);
echo $model->getProfilePhotoPath();

Probably be good to add some exception checking in some places but that would be the gist of it.

Lastly, model is probably a bad name. It looks like your dealing with users if so use user instead. Name the classes and table based the context from the domain, not something generic like “model”. Naming something so generically is likely to cause confusion and/or conflict down the line. Try to be as specific and consistent with naming as possible without getting to convoluted. That will make the code easier to read later down the line for everyone involved.

Thanks guys, I appreciate the help.

Oddz, your example worked. I Googled object class mapper and couldn’t really find any examples or tutorials on the method you used - I found results on ORM (which I’m using) and actual maps, which isn’t so helpful. :wink:

I’d like to know more about this. Is there a site/article you could recommend to me?

Thanks again!