0

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> &rarr; '.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?

2
  • 2
    rrmdir() is a method in your class, so use $this->rrmdir() Commented Jan 5, 2014 at 17:26
  • please note that you have to calls to rrmdir(). All them has to be $this->rrmdir(), probably you are just changing one of them. Commented Jan 5, 2014 at 17:30

1 Answer 1

2

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

2 Comments

Thank you! I can't believe it was so easy! I was frustrated for a whole day because of this. Thanks again!
Haha sure, it's the little things sometimes ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.