Reputation:
My problem is that I have two functions and I try to call one in the other but I get the error it is undefined. Here is my code:
class LoggedUser extends User {
public function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
public function usunGalerie($galeria,$log,$folder){
require_once('db_connect.inc');
$query = "DELETE FROM galery WHERE Nazwa='$galeria' AND Autor='$log';";
$result = @mysql_query($query);
if(!$result = mysql_query($query))
{
echo '<code>$query</code> → '.mysql_error().' ('.mysql_errno().')';
}
$query = "DELETE FROM zdjecie WHERE Galeria='$galeria' AND Autor='$log';";
$result2 = @mysql_query($query);
if ($result && $result2) {
echo 'Usunięto galerię '.$galeria;
$this->rrmdir($folder);
} else {
echo 'Wystąpił błąd.';
}
}
}
then I get: Call to undefined function rrmdir() I tried to call it without $this-> but it's the same.
Please can anyone help me?
Upvotes: 0
Views: 2104
Reputation: 3251
Once you enter the world of Object Oriented PHP, the way scoping works changes. Once you have declared an instance of your class you now have to reference the instance of the class as well as the function itself. This does work differently in other languages (like Java) where methods are assumed to be called within the instance itself in a non static context.
The following line calls the function again
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
Even thought it's inside the function it's calling (recursive, be careful) you still need to refer to the object instance. Make sure ALL calls to rrmdir include $this
if (filetype($dir."/".$object) == "dir") $this->rrmdir($dir."/".$object); else unlink($dir."/".$object);
Upvotes: 2