How to use method_exist()

Asked

Viewed 81 times

0

Good people, I want to use the method_exist(), I’m trying to pass values to my class in PHP, in URL that need to call the class it is necessary to call the method to where the values go.

How’s a file to handle it ?

1 answer

2


I didn’t quite understand the question, but here’s what function is and how to use it.

The function method_exists() only checks if a method exists:

Description:

bool method_exists ( object $object , string $method_name )
  • Returns bool (true whether the method has been found and false if not);
  • Gets a Object (in case it would be your class);
  • Also receives a string (with the name of the method);

Definition:

if(method_exists('MinhaClasse','MeuMetodo')) {
    echo "existe";
} else {
    echo "não existe";
}

Example:

<?php
    $directory = new Directory('.');
    var_dump(method_exists($directory,'read')); //retorna true pois existe o método read na classe de diretório
?>

The same can see done so:

<?php
    var_dump(method_exists('Directory','read'));
?>

Example:

<?php
    class A {
        public function FUNC() {
            echo '*****'; 
        }
    }

    $a = new A();
    $a->func(); // printa *****
    var_dump(method_exists($a, 'func')); // retorna bool(true) pois existe tal método
?>

See more: PHP documentation: method_exists()

  • 1

    Thanks for that summary, it helped a lot !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.