Welcome Guest, Not a member yet? Register   Sign In
how check method exist
#1

[eluser]minhbu[/eluser]
i want to check a method exists in my class but i don`t know to used any function ?
Help me
#2

[eluser]Colin Williams[/eluser]
[url="http://www.php.net/method_exists"]http://www.php.net/method_exists[/url]
#3

[eluser]minhbu[/eluser]
i don`t know used it. I know to used it in OOP and in codeigniter is not !
#4

[eluser]Colin Williams[/eluser]
I love your response. It's like a puzzle. Here's how you use it (and this has nothing to do with OOP.. it's just a function that says whether the given object has the given method).

Code:
class Frog {

   function Frog()
   {
      print method_exists($this, 'lick') ? 'Frog can lick.' : 'Frog cannot lick';
   }

   function lick()
   {
      print 'Licked.'
   }

}
#5

[eluser]minhbu[/eluser]
thanks, but you.................. shit
#6

[eluser]minhbu[/eluser]
ahhhhhhh
i have got a problem.
$b="lick";
$this->$b;

Undefined property: Frog::$b
#7

[eluser]Colin Williams[/eluser]
You actually have many problems with PHP. I suggest reading the PHP manual at php.net or getting a book.

But, just to answer this new question, $b is a variable in that scope (which I am assuming is inside a class member function), not a property.

Code:
$this->b = "lick";
print $this->b
#8

[eluser]Colin Williams[/eluser]
Oh.. and you're right. I absolutely do use the restroom on occasion.
#9

[eluser]minhbu[/eluser]
No
i have a controller demo
class Demo extends Controller{
function demo()
{
parent::Controller();
}
function index()
{
$id=$this->uri->segment(3);
if(method_exists($this,$id))
$this->$id;
else
echo "Your method haven`t got";
}
function a()
{
echo "Hello";
}
}
I have URI http://localhost/demo/index/a
but i have a error

A PHP Error was encountered
Severity: Notice

Message: Undefined property: Demo::$id

Filename: controllers/demo.php

Line Number: 20
#10

[eluser]Colin Williams[/eluser]
You are calling a function, so you need parenthesis

Code:
function index()
{
   $id=$this->uri->segment(3);
   if(method_exists($this,$id))
       $this->$id();
   else
       echo “Your method haven`t got”;
}




Theme © iAndrew 2016 - Forum software by © MyBB