PHP - Calling Methods from Class

Introduction

To call an object's method, use the following format:

$object->method();

Here's a simple example that creates a class with a method, then creates an object from the class and calls the object's method:

Demo

<?php
        class MyClass {

          public function hello() {
            echo"Hello, World!";
          }/*from  w  w w .  ja va2s  .c o m*/
         }

         $obj = new MyClass;
         $obj->hello();  // Displays"Hello, World!"
?>

Result

Related Topic