13 OOP – IV in PHP

Hiren Joshi

epgp books

 

 

 

 

Abstract class and method

  • An abstract class is a class which cannot be used to create an object.
  • An abstract class is used to create a superclass which can be inherited by other classes. But an object from superclass (Abstract class) cannot be created.
  • An abstract method is a method which has only method name and parameter(s) but does not have code blocks which implements the method. In short, abstract method has defined but it cannot have implementation.
  • An abstract method can only defined in abstract class.
  • An Abstract class can have abstract and non-abstract method(s).
  • A concrete class is a class which cane used to create an object.
  • The concrete class which inherits the abstract class must implement all abstract methods of abstract class. Otherwise, PHP shows fatal error.

 

Following code shows an example of abstract class.

 

<?php

abstract class Person

{

private $fname,$lname,$email,$mobile;

public function __construct($fname,$lname)

{

$this->fname = $fname;

$this->lname = $lname;

}

 

//fname

 

public function getfname()

{

return $this->fname;

}

public function setfname($value)

{

$this->fname = $value;

}

 

//lname

 

public function getlname()

{

return $this->lname;

}

public function setlname($value)

{

$this->lname = $value;

}

 

//email

 

public function getemail()

{

return $this->email;

}

public function setemail($value)

{

$this->email = $value;

}

 

//mobile

     public function getmobile()

{

return $this->mobile;

}

public function setmobile($value)

{

$this->mobile = $value;

}

 

//fullname

 

abstract public function getfullname();

}

class Employee extends Person

{

private $eid, $edept;

public function __construct($fname, $lname,$eid,$edept)

{

$this->eid = $eid;

$this->edept = $edept;

parent::__construct($fname,$lname);

}

 

//eid

 

public function geteid()

{

return $this->eid;

}

public function seteid($value)

{

$this->eid = $value;

}

 

//edept

 

public function getdept()

{

return $this->edept;

}

public function setedept($value)

{

$this->edept = $value;

}

 

//getfullname

 

public function getfullname()

{

$fullname = $this->getfname().’ ‘.$this->getlname();

return $fullname;

}

}

$emp = new Employee(‘first’,’last’,1,’IT’);

print_r($emp);

$ans = $emp->getfullname();

echo “<br> $ans”;

?>

 

The output will be:

 

Employee Object ( [eid:Employee:private] => 1 [edept:Employee:private] => IT [fname:Person:private] => first [lname:Person:private] => last [email:Person:private] => [mobile:Person:private] => ) first last

 

Final class and method

  • Sometimes you may be required that a method does not override in subclass. To do that, you can use the final keyword.
  • Sometimes you may be required that a class does not inherited by a subclass. To do that, you can use the final keyword.
  • Final class cannot be extended by a subclass.

Following example shows the use of final method.

 

<?php

class Person

{

private $fname,$lname,$email,$mobile;

public function __construct($fname,$lname)

{

$this->fname = $fname;

$this->lname = $lname;

}

 

//fname

 

public function getfname()

{

return $this->fname;

}

public function setfname($value)

{

$this->fname = $value;

}

 

//lname

 

public function getlname()

{

return $this->lname;

}

public function setlname($value)

{

$this->lname = $value;

}

 

//email

 

public function getemail()

{

return $this->email;

}

public function setemail($value)

{

$this->email = $value;

}

 

//mobile

 

public function getmobile()

{

return $this->mobile;

}

public function setmobile($value)

{

$this->mobile = $value;

}

final public function getfullname()

{

echo “<h1> Hello”;

}

}

class Employee extends Person

{

private $eid, $edept;

public function __construct($fname, $lname,$eid,$edept)

{

$this->eid = $eid;

$this->edept = $edept;

parent::__construct($fname,$lname);

}

 

//eid

 

public function geteid()

{

return $this->eid;

}

public function seteid($value)

{

$this->eid = $value;

}

 

//edept

 

public function getdept()

{

return $this->edept;

}

public function setedept($value)

{

$this->edept = $value;

}

public function getfullname()

{

return $this->getfname(). ‘ ‘.$this->getlname();

}

}

$emp = new Employee(‘first’,’last’,1,’IT’);

print_r($emp);

$emp->setmobile(1234567890);

echo “<br>”;

print_r($emp);?>

 

The output will be:

 

Fatal error: Cannot override final method Person::getfullname() in :\wamp\www\PathshalaWAD\OOP\Final method\final method.php on line 92

 

If you define

 

<?php

final class Person

{

private $fname,$lname,$email,$mobile;

public function __construct($fname,$lname)

{

$this->fname = $fname;

$this->lname = $lname;

}

//fname

public function getfname()

{

return $this->fname;

}

public function setfname($value)

{

$this->fname = $value;

}

//lname

public function getlname()

{

return $this->lname;

}

public function setlname($value)

{

$this->lname = $value;

}

//email

public function getemail()

{

return $this->email;

}

public function setemail($value)

{

$this->email = $value;

}

//mobile

 

public function getmobile()

{

return $this->mobile;

}

public function setmobile($value)

{

$this->mobile = $value;

}

final public function getfullname()

{

echo “<h1> Hello”;

}

}

class Employee extends Person

{

private $eid, $edept;

public function __construct($fname, $lname,$eid,$edept)

{

$this->eid = $eid;

$this->edept = $edept;

parent::__construct($fname,$lname);

}

//eid

public function geteid()

{

return $this->eid;

}

public function seteid($value)

{

$this->eid = $value;

}

//edept

public function getdept()

{

return $this->edept;

}

public function setedept($value)

{

$this->edept = $value;

}

public function getfullname()

{

return $this->getfname(). ‘ ‘.$this->getlname();

}

}

$emp = new Employee(‘first’,’last’,1,’IT’);

print_r($emp);

$emp->setmobile(1234567890);

echo “<br>”;

print_r($emp);?>

 

Then the output will be:

 

Fatal error: Class Employee may not inherit from final class (Person) in \wamp\www\PathshalaWAD\OOP\Final method\final class demo.php on line 92

 

Interface

  • Some OOP language like C++ supports multiple inheritance.
  • Multiple inheritance means a class can simultaneously inherits more than one class.
  • In PHP, a class cannot simultaneously inherits more than one class.
  • A class can inherit multiple interfaces simultaneously in PHP.
  • An interface defines public method(s) which can be implemented by a class.
  • The interface only defines the method (method name and parameter list) but does not provide method implementation (code to implement method).
  • Each method in interface must be public.
  • Method(s) in interface cannot be static.
  • A class which implements interface must provide implementation of all the methods define in interface.
  • An interface can define class constant also. These class constants can be available to any class which implements interface.
  • A class can only inherit one class but it can implements many interfaces.
  • A class can inherit one class and multiple interfaces simultaneously.

Following code shows example of  Interface.

 

<?php

//  interface showable interface showable

{

public function show();

}

//interface test

interface test

{

public function disp1($val1);

public function disp2($val1, $val2);

}

//interface gender

interface gender

{

const M = ‘Male’;

const F = ‘Female’;

}

//  class person class Person

{

private $fname,$lname,$email,$mobile;

public function __construct($fname,$lname)

{

$this->fname = $fname;

$this->lname = $lname;

}

//fname

public function getfname()

{

return $this->fname;

}

public function setfname($value)

{

$this->fname = $value;

}

//lname

public function getlname()

{

return $this->lname;

}

public function setlname($value)

{

$this->lname = $value;

}

//email

public function getemail()

{

return $this->email;

}

public function setemail($value)

{

$this->email = $value;

}

//mobile

public function getmobile()

{

return $this->mobile;

}

public function setmobile($value)

{

$this->mobile = $value;

}

}

class Employee extends Person implements showable,test

{

private $dept, $salary;

public function __construct($fname,$lname,$dept,$salary)

{

parent::__construct($fname,$lname);

$this->dept = $dept;

$this->salary = $salary;

}

//dept

public function getdept()

{

return $this->dept;

}

public function setdept($value)

{

$this->dept = $value;

}

//salary

public function getsalary()

{

return $this->salary;

}

public function setsalary($value)

{

$this->salary = $value;

}

//  implements show mrthod of interface showable public function show()

{

echo “<br> Full Name :”. $this->getfname().’ ‘.$this->getlname(); echo “<br> Dept :”.$this->dept;

echo “<br> Salry :”.$this->salary;

}

public function disp1($val)

{

echo “<h1> This is disp1 function : $val”;

}

public function disp2($val1,$val2)

{

echo “<br> Calling Disp2 to display $val1 and $val2”;

}

}

$emp = new Employee(‘First’,’Last’,’IT’,25000);

echo “<br>”;

emp->show();

echo “<br>”;

$emp->disp2(‘Good Morning’,’from Disp2′);

echo “<br>”;

$emp->disp1(‘Greetings’);

?>

 

The output will be:

 

Full Name :First Last

Dept :IT

Salry :25000

Calling Disp2 to display Good Morning and from Disp2

 

This is disp1 function : Greetings

you can view video on OOP – IV 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 Using HTML/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/