1

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; } } 

When running this class I have this error:

Fatal error: Call to undefined function 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?

2 Answers 2

3

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]); 
Sign up to request clarification or add additional context in comments.

2 Comments

But why $values[$i] = mysql_real_escape_string ($values[$i]); has not this error?
@phpExe, because mysql_real_escape_string isn't a member function of your class, it's just a library function available to you.
1

You need to change the call to $values[$i] = $this->safe_value($values[$i]); for non-static methods. For static methods, use $values[$i] = database::safe_value($values[$i]).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.