OOP Tweaks: Class Variables Constructor

Objectives:

  • public method and variable (property) used.
  • class constructor is created as
    • function name_of_the_class()  {    echo "whatever do display "; }
  • class constructor fires when an instance of the class is created with new operator.
  • Call  public variable with obect->no_dollar_sign_varable-name.
  • Call public method with object->function_name();

Code

<?php
//object not encapsulated with a class
class test_object
{
public $var1 = " <font style='color:red;'>
object oriented programming</font>";
public $required = array('age', 'name', 'comments');
function test_object()
{
echo "<br/><font style='color:Green;'>
constructor evoked</font>";
}
function _construct()
{
echo "<br/><font style='color:red;'>
function responded OK<font>";
}
}
echo 'This prints nothing'. $var1;
echo '<br/>Creating instance of a class';
$obj1= new test_object();
$obj3= new test_object();
echo '<br/> Instance Created';
echo '<br/>Public Object : $obj1->$var1';
$obj2 = $obj1->var1;
echo "<br/>public variable $obj2";
echo '<br/> Calling a function $obj1->_construct()';
$obj1->_construct();

?>

Runtime view