Tuesday, April 20, 2010

Workshop 5 Working with Objects in PHP

OBJECTS

An object is typically a sort of container that consists of
variables
functions
etc
An object in php is similar to a class object in JAVA

<?php

class lecturers {
    var $name = "Yann";
    var $subjectcode  = "ITC382" ;
    var $subjectname = "Client Server Applications";
    }

$mylecturer = new lecturers();
echo "My name is ".$mylecturer -> name. " and I teach " .$mylecturer -> subjectcode." " .$mylecturer -> subjectname;

?>

You can also change the properties of an object in the code as illustrated.

<?php

class lecturers {
    var $name = "Yann";
    var $subjectcode  = "ITC382" ;
    var $subjectname = "Client Server Applications";
    }

$mylecturer = new lecturers();
echo "My name is ".$mylecturer -> name. " and I teach " .$mylecturer -> subjectcode." " .$mylecturer -> subjectname;


// changing the object properties
echo "<br> This is to relfect the changes in object properties <br>";
$mylecturer -> name = "Sam";
$mylecturer -> subjectcode = "ITC211";
$mylecturer -> subjectname = "Multimedia Systems";

echo "My name is ".$mylecturer -> name. " and I teach " .$mylecturer -> subjectcode." " .$mylecturer -> subjectname;

?>

You can also add methods into your class objects as illustrated.

<?php

class displayname {
    function name() {
            echo "My name is Yann " ;
            } 
       }

$name = new displayname();
$name  -> name();
?>

0 comments:

Post a Comment