Page 326 - Computer Software Application TP - Volume 1
P. 326

COMPUTER SOFTWARE APPLICATION - CITS





           EXERCISE 59 : Demonstrate                            working             with        Classes

                                     And   Objects - Creating an object, Object
                                     properties,             Object          methods,              Object

                                     constructors and destructors


           Objectives

           At the end of this exercise you shall be able to
           •  create object in PHP
           •  use object properties in PHP
           •  use object constructors and destructors in PHP.

           Requirements


           Tools/Materials
           •   Computer/Laptop with latest configuration       •  PHP
           •   Operating system: windows 10 or 11              •  Text editor
           •  Apache web server                                •  Web browser


           Procedure

           TASK 1: Creating an object, properties and methods

              1  Open the text editor
              2  Write the following codes
                <html >
                   <head>

                         <title> Creating an object  </title>
                   </head>
                   <body>
                          <?php
                         class Person

                         {
                                                            //Add Properties
                                public $name;
                                public $age;

                                //Add Methods
                                public function greet()
                                {
                                        echo “Hello, my name is “ . $this->name . “.”;
                                }
                         }

                         //Create an Object Instance


                                                           311
   321   322   323   324   325   326   327   328   329   330   331