CodeIgniter Forums
how check method exist - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how check method exist (/showthread.php?tid=12569)

Pages: 1 2


how check method exist - El Forum - 10-23-2008

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


how check method exist - El Forum - 10-23-2008

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


how check method exist - El Forum - 10-23-2008

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


how check method exist - El Forum - 10-24-2008

[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.'
   }

}



how check method exist - El Forum - 10-24-2008

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


how check method exist - El Forum - 10-24-2008

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

Undefined property: Frog::$b


how check method exist - El Forum - 10-24-2008

[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



how check method exist - El Forum - 10-24-2008

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


how check method exist - El Forum - 10-24-2008

[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


how check method exist - El Forum - 10-24-2008

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