从类中调用函数或方法 [英] Call a function or method from inside its class
本文介绍了从类中调用函数或方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个数据库类,但无法从内部访问该方法:
I have written a database class but can't access the method from within:
class database{
.
.
private $mgc;
private $real;
public function insert($table,$values,$row = null){
.
.
for($i = 0; $i < count($values); $i++){
$values[$i] = safe_value ($values[$i]);
}
.
.
}
public function safe_value( $value ) {
if( $this->real ) {
if( $this->mgc ) { $value = stripslashes( $value ); }
$value = mysql_real_escape_string( $value );
}
else {
if( !$this->mgc ) { $value = addslashes( $value ); }
}
return $value;
}
}
运行此类时,错误:
致命错误:调用未定义函数safe_value()
Fatal error: Call to undefined function safe_value()
当我使用 mysql_real_escape_string
而不是 safe_value
方法时,该类工作得很好。为什么我无法访问 safe_value
函数,为什么会显示此错误?
When I used mysql_real_escape_string
instead of the safe_value
method the class works perfectly. Why can't I access the safe_value
function and why is it showing me this error?
推荐答案
在类中调用非静态成员函数时,需要使用 $ this
来引用它,在这种特殊情况下,
When calling a non-static member function from within the class you need to refer to it using $this
, in this particular case you should call it as
$values[$i] = $this->safe_value($values[$i]);
这篇关于从类中调用函数或方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文