11 OOP – II in PHP

Hiren Joshi

epgp books

 

 

 

Class Constant:

 

A class constant is a constant which define inside a class. Class constants are always public. You cannot use access modifier with class constant. As class constants are public, they can be used outside the class. Class constant belongs to a class, not to the object. Hence a class constant can directly access from the class without creating an object of the class first.

 

Class constants are typically used to provide a fixed value from the available set of option.

 

Syntax for creating class constant:

 

const <constantName> =  <constant value>

  • Constant value must be a scalar value or array of scalar value.
  • Scalar value means literal number, string or Null.

 

To use the constant inside a class, you can code keyword self-followed by double colon (::) and constant name.

 

To use the constant outside a class, you can code class name followed by double colon (::) and constant name

 

The double colon (::) is known as scope resolution operator. Some error message mat refer double colon as Paamayim Nekudotayim. Paamayim Nekudotayim is a Hebrew word which means double colon.

 

The following code shows an example using class constant.

 

<?php

class Employee

{

private $dept;

const H = ‘HR’;

const A = ‘Admin’;

public function getdept()

{

return $this->dept;

}

public function setdept($value)

{

if($value == self::H || $value == self::A)

    {

$this->dept = $value;

}

else

{

exit(‘Invalid Department’);

}

}

}

$emp = new Employee();

$emp->setdept(Employee::A);

echo ‘<br> You are from ‘. $emp->getdept() .’ Department ‘;

// Or you can write

echo ‘<br> You are from ‘. Employee::H .’ Department ‘; ?>

 

Then the output is

 

You are from Admin Department

You are from HR Department

 

Static Properties and Methods:

 

Static properties and methods belong to the class, not to an object. So it is also known as class properties and class methods. This way it is very similar to class constant. However, static properties and methods differentiate from class constant mainly by two things.

 

1. Access modifier (public, private or protected) can be coded with static properties and methods when you define it while for class constant you cannot write access modifier.

2.Static properties and methods do not belong to an object. Hence $this variable is not used inside a static method to access the current object. Although keyword – self – can be used to access the current class. As a result, a static method can only work with other static properties, static methods and class constants.

  • To use the static property or static method inside a class, you can code keyword self-followed by double colon (::) and static property or method name.
  • To use the static property outside a class, you can code class name followed by double colon (::) and static property or method name.

 

Let us take one example to demonstrate the use of static method.

 

<?php

class Employee

{

private $eid,$ename;

public static $objcount = 0; //define a static properties public $total = 0;

public function __construct($eid,$ename)

{

$this->eid = $eid;

$this->ename = $ename; self::$objcount++;

}

//eid

public function  geteid()

{

return $this->eid;

}

public function seteid($value)

{

$this->eid = $value;

}

//ename

public function getename()

   {

return $this->ename;

}

public function setename($value)

{

$this->ename = $value;

}

public static function objectcount()

{

return self::$objcount

}

}

$obj1 = new Employee(1,’One’);

echo “<br>”.Employee::objectcount();

$obj2 = new Employee(2,’Two’);

echo “<br>”.Employee::$objcount ;

$obj3 = new Employee(3,’Three’);

echo “<br>”.Employee::objectcount();

$obj4 = new Employee(4,’Four’);

echo “<br>”.Employee::objectcount();

 

Then the output will be:

 

1

2

3

4

 

Loop through an object’s properties

  • Foreach loop is used to loop through an object’s each properties.
  • The syntax of foreach loop through an object’s properties

 

foreach ($objectName as [$propertyName => ] $propertyValue)

{

Statement(s);

}

 

  • A foreach loop is coded inside a method of an object loops through object’s private, protected and public properties.
  • A foreach loop is coded outside of an object loops through only object’s public properties.

 

The following example demonstrate the use of foreach loop.

 

<?php

class Employee

{

public $eid,$ename;

private $edept, $esal;

public function __construct($eid,$ename)

{

$this->eid = $eid;

$this->ename = $ename;

}

//eid

public function geteid()

{

return $this->eid;

}

public function seteid($value)

{

$this->eid = $value;}

//ename

public function getename()

{

return $this->ename;

}

public function setename($value)

{

$this->ename = $value;

}

//edat

public function getedept()

{

return $this->edept;

}

public function setedept($value)

{

$this->edept = $value;

}

//esal

public function getesal()

{

return $this->esal;

}

public function setesal($value)

{

$this->esal = $value;

}

//show

public function show()

{

echo “<ul>”;

foreach($this as $name => $value)

{

echo “<li> $name : $value “;

}

echo “</ul>”;

}

}

$e = new Employee(1,’One’);

$e->setedept(‘IT’);

$e->setesal(175000);

$e->show();

echo “<br> Outside class <br>”;

echo “<ul>”;

foreach($e as $k=>$v)

{

echo “<li> $k : $v </li>”;

}

echo “</ul>”;

?>

 

The output of the above program is :

  • eid : 1
  • ename : One
  • edept : IT
  • esal : 175000

 

Outside class

  • eid : 1
  • ename : One
you can view video on OOP – II in PHP

Reference:

 

1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,

2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress

3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, apache, and MySQL Web Development, Wrox,

4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O’Reilly Media

5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP, Wrox

6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible

7. Joel Murach, Ray Harris: Murach’s PHP and MySQL, Shroff/Murach

8. Ivan Bayross, Web Enabled Commercial Application Development UsingHTML/Javascript/DHTML/PHP , BPB Publications

9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams

10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education

11. http://www.php.net/

12. http://www.w3schools.com/

13. http://www.tutorialspoint.com/

14. https://docs.oracle.com/javase/tutorial/java/concepts/

15. http://www.tutorialspoint.com/cplusplus/cpp_object_oriented.htm

16. http://www.tutorialspoint.com/php/php_object_oriented.htm

17. http://inchoo.net/dev-talk/understanding-phps-oop-basic-terms-explained/