调用非对象上的成员函数 - PHP [英] Call To A Member Function On A Non-Object - PHP

查看:19
本文介绍了调用非对象上的成员函数 - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以显示、修复或告诉我为什么我会收到此错误

Can someone show, fixed or tell me why i am getting this error

在非对象上调用成员函数 getRating()

这是函数

public function getRating($movieid){
    $movieid = mysql_real_escape_string($movieid);
    $average = 0;
    $e = mysql_query("SELECT AVG(`rating`) as average FROM movie_ratings WHERE movieid='$movieid'") or die(mysql_error());
    if (mysql_num_rows($e)>0){
      extract(mysql_fetch_array($e));
    }
    return $average;
}

这就是我调用函数的方式

And This is how i call the function

$movie = $movie->getByPerma($perma,$language);

if (empty($movie)){
    $movie = '';
} else {

    $movie['rating'] = $movie->getRating($movie['id']);
    $tags = $movie->getMovieCategoryDetails($movie['id'],$language);
    if (count($tags)){
        $smarty->assign("tags",$tags);
    } else {
        $smarty->assign("tags","");
    }

}

有人可以帮助我,因为我是 php 新手.

Can someone help me as i'm new to php.

推荐答案

$movie->getByPerma($perma,$language); 

正在返回一些不是对象的东西.

is returning something that is not an object.

所以我会

print_r($movie)

在第 2 行,看看我得到了什么.

on line 2 and see what I'm getting.

第二个奇怪的事情是:

$movie['rating'] = $movie->getRating($movie['id']);

在左侧,您将 $movie 用作数组,而在右侧,您将其用作对象,然后再次发送您使用 $movie['id'] 作为数组的参数.

On the left side you are using $movie as an array and on the right side you are using it as an object and then again yo sent the parameter you use $movie['id'] as an array.

所以:

如果你得到一个数组,这个数组不能有函数,这个函数应该在一个类之外,并且会像这样被调用:

If you are getting an array, the array cant have functions, the function should be outside a class and will be called like this:

getRating($movie['id']) 

代替

$movie->getRating($movie['id']).

如果您正在获取一个对象,并且该对象实现了该功能

If you are getting an object, and the object implements the function

getRating($movie_id)

那么访问对象属性的方式是:

then the way to access the properties of the object is:

$movie->rating and $movie->id

我假设这些属性是公开的.但这不是正确的做法......属性应该是私有的,你应该为这样的对象属性实现getter和setter:

I'm asuming that the properties are declared public. This is not the correct way of doing it though... The properties should be private and you should implemente getters and setters for the objects properties like this:

 private $rating;
 public function get_rating()
{
  return $this->rating; 
}

在这种情况下,要获得评分,请使用

In this case to get the rating, use

 $movie->get_rating();

并为评级分配一个值,实施

And to asign a value to the rating, implement

  public function set_rating($r)
  {
     $this->rating=$r; 
  }

并像这样分配值:

$movie->set_rating($some_rating);

不知道我是否帮助或使一切变得更加混乱:S,但请随时问我问题:)

Dunno if I helped or made everything more confusing :S but feel free to ask me questions :)

这篇关于调用非对象上的成员函数 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆