Thursday, January 10, 2013

Adding a Method

The Demo class isn't particularly useful if it isn't able to do anything,so let's look at how you can create a method.Remember,a method of a class is basically just a function.By coding a function inside the braces of your class,you are adding a method to that class . Here 's an example:

saved as class.demo.php
 <?php
        class Demo{
        function sayHello($name){
            print "Hello $name!";
         }
   }
?>


saved as testdemo.php
 <?php
        require_once('class.demo.php');
        $objDemo=new Demo();
        $objDemo->sayHello('Visiter');
?>

The object is now capable of printing a friendly greeting.The -> operator is used to access all methods and properties of your objects.


No comments:

Post a Comment