Jump to content

PHP Class using another class, variable doesnt handles function

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Aulto

Aulto

    Newbie

  • Members
  • Pip
  • 2 posts
Hey, buddies.
I don't know how exactly to explain that, but i'm creating a class, which uses the TMDB Class as support for getting movie information. The code is:

<?php

class MovInfo

{	

	const API_KEY = 'apikey';

	public $tmdb;

	

	function __construct(){

		include 'config.php';

		include 'TMDb.php';

		$this->tmbd = new TMDb(MovInfo::API_KEY);

	}

	

	public function objectToArray($object)

	{

    if (!is_object($object) && !is_array($object)) {

        return $object;

    }

    if (is_object($object)) {

        $object = get_object_vars($object);

    }

    return array_map('objectToArray', $object);

	}

	

	public function getCover($id){

		$images = $this->tmdb->getImages($id);

		$img = $this->objectToArray(json_decode($images));

		return $img[0]['posters'][2]['image']['url'];

	}

}

?>

At this part of the code:
$images = $this->tmdb->getImages($id);

Returns the error:
Fatal error: Call to a member function getImages() on a non-object in /opt/lampp/htdocs/get.php on line 25

I don't know what's wrong, how should i call this function.

Thank you.

#2
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
On line 10, you spelled your class variable wrong. It should be
$this->tmdb = [COLOR=#000000][COLOR=#0000BB][/COLOR][COLOR=#007700] new [/COLOR][COLOR=#0000BB]TMDb[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]MovInfo[/COLOR][COLOR=#007700]::[/COLOR][COLOR=#0000BB]API_KEY[/COLOR][COLOR=#007700]); [/COLOR][/COLOR]

What happens is that even though the property on that class was never declared, it will still create a property on that object to whatever you try to call. So on line 25, it expected tmdb to be an object and currently it's not set to any value.

#3
Aulto

Aulto

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks. That was a real silly mistake.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users