{"id":155,"date":"2018-07-12T10:23:44","date_gmt":"2018-07-12T10:23:44","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=155"},"modified":"2018-12-07T10:46:46","modified_gmt":"2018-12-07T10:46:46","slug":"oop-ii-in-php","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/oop-ii-in-php\/","title":{"rendered":"OOP &#8211; II in PHP"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/SOMUlzIELBs\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Class Constant:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\nClass constants are typically used to provide a fixed value from the available set of option.\r\n\r\n&nbsp;\r\n\r\n<strong>Syntax for creating class constant:<\/strong>\r\n\r\n&nbsp;\r\n\r\nconst &lt;constantName&gt; =\u00a0 &lt;constant value&gt;\r\n<ul>\r\n \t<li>Constant value must be a scalar value or array of scalar value.<\/li>\r\n \t<li>Scalar value means literal number, string or Null.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n<p style=\"text-align: justify\">To use the constant inside a class, you can code keyword self-followed by double colon (::) and constant name.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">To use the constant outside a class, you can code class name followed by double colon (::) and constant name<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\nThe following code shows an example using class constant.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\nclass Employee\r\n\r\n{\r\n\r\nprivate $dept;\r\n\r\nconst H = 'HR';\r\n\r\nconst A = 'Admin';\r\n\r\npublic function getdept()\r\n\r\n{\r\n\r\nreturn $this-&gt;dept;\r\n\r\n}\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">public function setdept($value)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($value == self::H || $value == self::A)<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 {\r\n\r\n$this-&gt;dept = $value;\r\n\r\n}\r\n\r\nelse\r\n\r\n{\r\n\r\nexit('Invalid Department');\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n\r\n$emp = new Employee();\r\n\r\n$emp-&gt;setdept(Employee::A);\r\n\r\necho '&lt;br&gt; You are from '. $emp-&gt;getdept() .' Department ';\r\n\r\n\/\/ Or you can write\r\n\r\necho '&lt;br&gt; You are from '. Employee::H .' Department '; ?&gt;\r\n\r\n&nbsp;\r\n\r\nThen the output is\r\n\r\n&nbsp;\r\n\r\nYou are from Admin Department\r\n\r\nYou are from HR Department\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Static Properties and Methods:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n<p style=\"text-align: justify\">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 \u2013 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.<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\">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.<\/li>\r\n \t<li style=\"text-align: justify\">To use the static property outside a class, you can code class name followed by double colon (::) and static property or method name.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nLet us take one example to demonstrate the use of static method.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\nclass Employee\r\n\r\n{\r\n\r\nprivate $eid,$ename;\r\n\r\npublic static $objcount = 0; \/\/define a static properties public $total = 0;\r\n\r\npublic function __construct($eid,$ename)\r\n\r\n{\r\n\r\n$this-&gt;eid = $eid;\r\n\r\n$this-&gt;ename = $ename; self::$objcount++;\r\n\r\n}\r\n\r\n\/\/eid\r\n\r\npublic function\u00a0 geteid()\r\n\r\n{\r\n\r\nreturn $this-&gt;eid;\r\n\r\n}\r\n\r\npublic function seteid($value)\r\n\r\n{\r\n\r\n$this-&gt;eid = $value;\r\n\r\n}\r\n\r\n\/\/ename\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">public function getename()<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0{\r\n\r\nreturn $this-&gt;ename;\r\n\r\n}\r\n\r\npublic function setename($value)\r\n\r\n{\r\n\r\n$this-&gt;ename = $value;\r\n\r\n}\r\n\r\npublic static function objectcount()\r\n\r\n{\r\n\r\nreturn self::$objcount\r\n\r\n}\r\n\r\n}\r\n\r\n$obj1 = new Employee(1,'One');\r\n\r\necho \"&lt;br&gt;\".Employee::objectcount();\r\n\r\n$obj2 = new Employee(2,'Two');\r\n\r\necho \"&lt;br&gt;\".Employee::$objcount ;\r\n\r\n$obj3 = new Employee(3,'Three');\r\n\r\necho \"&lt;br&gt;\".Employee::objectcount();\r\n\r\n$obj4 = new Employee(4,'Four');\r\n\r\necho \"&lt;br&gt;\".Employee::objectcount();\r\n\r\n&nbsp;\r\n\r\nThen the output will be:\r\n\r\n&nbsp;\r\n\r\n1\r\n\r\n2\r\n\r\n3\r\n\r\n4\r\n\r\n&nbsp;\r\n\r\n<strong>Loop through an object\u2019s properties<\/strong>\r\n<ul>\r\n \t<li>Foreach loop is used to loop through an object\u2019s each properties.<\/li>\r\n \t<li>The syntax of foreach loop through an object\u2019s properties<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nforeach ($objectName as [$propertyName =&gt; ] $propertyValue)\r\n\r\n{\r\n\r\nStatement(s);\r\n\r\n}\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li style=\"text-align: justify\">A foreach loop is coded inside a method of an object loops through object\u2019s private, protected and public properties.<\/li>\r\n \t<li style=\"text-align: justify\">A foreach loop is coded outside of an object loops through only object\u2019s public properties.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nThe following example demonstrate the use of foreach loop.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">class Employee<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public $eid,$ename;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">private $edept, $esal;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function __construct($eid,$ename)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$this-&gt;eid = $eid;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$this-&gt;ename = $ename;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/eid<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function geteid()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">return $this-&gt;eid;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function seteid($value)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$this-&gt;eid = $value;<\/span><span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/ename<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function getename()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">return $this-&gt;ename;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function setename($value)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$this-&gt;ename = $value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/edat<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function getedept()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">return $this-&gt;edept;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function setedept($value)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$this-&gt;edept = $value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/esal<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function getesal()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">return $this-&gt;esal;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function setesal($value)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$this-&gt;esal = $value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/show<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public function show()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;ul&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">foreach($this as $name =&gt; $value)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;li&gt; $name : $value \";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;\/ul&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$e = new Employee(1,'One');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$e-&gt;setedept('IT');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$e-&gt;setesal(175000);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$e-&gt;show();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Outside class &lt;br&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;ul&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">foreach($e as $k=&gt;$v)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;li&gt; $k : $v &lt;\/li&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;\/ul&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output of the above program is :\r\n<ul>\r\n \t<li>eid : 1<\/li>\r\n \t<li>ename : One<\/li>\r\n \t<li>edept : IT<\/li>\r\n \t<li><span style=\"text-align: initial;font-size: 1em\">esal : 175000<\/span><\/li>\r\n<\/ul>\r\n<\/div>\r\n&nbsp;\r\n\r\nOutside class\r\n<ul>\r\n \t<li>eid : 1<\/li>\r\n \t<li>ename : One<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on OOP \u2013 II in PHP<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/SOMUlzIELBs\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<strong>Reference:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP, Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development UsingHTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\r\n<p style=\"text-align: justify\">14. https:\/\/docs.oracle.com\/javase\/tutorial\/java\/concepts\/<\/p>\r\n<p style=\"text-align: justify\">15. http:\/\/www.tutorialspoint.com\/cplusplus\/cpp_object_oriented.htm<\/p>\r\n<p style=\"text-align: justify\">16. http:\/\/www.tutorialspoint.com\/php\/php_object_oriented.htm<\/p>\r\n<p style=\"text-align: justify\">17. http:\/\/inchoo.net\/dev-talk\/understanding-phps-oop-basic-terms-explained\/<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/SOMUlzIELBs\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Class Constant:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>Class constants are typically used to provide a fixed value from the available set of option.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax for creating class constant:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>const &lt;constantName&gt; =\u00a0 &lt;constant value&gt;<\/p>\n<ul>\n<li>Constant value must be a scalar value or array of scalar value.<\/li>\n<li>Scalar value means literal number, string or Null.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To use the constant inside a class, you can code keyword self-followed by double colon (::) and constant name.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To use the constant outside a class, you can code class name followed by double colon (::) and constant name<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>The following code shows an example using class constant.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>class Employee<\/p>\n<p>{<\/p>\n<p>private $dept;<\/p>\n<p>const H = &#8216;HR&#8217;;<\/p>\n<p>const A = &#8216;Admin&#8217;;<\/p>\n<p>public function getdept()<\/p>\n<p>{<\/p>\n<p>return $this-&gt;dept;<\/p>\n<p>}<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">public function setdept($value)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($value == self::H || $value == self::A)<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 {<\/p>\n<p>$this-&gt;dept = $value;<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>{<\/p>\n<p>exit(&#8216;Invalid Department&#8217;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>$emp = new Employee();<\/p>\n<p>$emp-&gt;setdept(Employee::A);<\/p>\n<p>echo &#8216;&lt;br&gt; You are from &#8216;. $emp-&gt;getdept() .&#8217; Department &#8216;;<\/p>\n<p>\/\/ Or you can write<\/p>\n<p>echo &#8216;&lt;br&gt; You are from &#8216;. Employee::H .&#8217; Department &#8216;; ?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Then the output is<\/p>\n<p>&nbsp;<\/p>\n<p>You are from Admin Department<\/p>\n<p>You are from HR Department<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Static Properties and Methods:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p style=\"text-align: justify\">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 \u2013 self &#8211; 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.<\/p>\n<ul>\n<li style=\"text-align: justify\">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.<\/li>\n<li style=\"text-align: justify\">To use the static property outside a class, you can code class name followed by double colon (::) and static property or method name.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Let us take one example to demonstrate the use of static method.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>class Employee<\/p>\n<p>{<\/p>\n<p>private $eid,$ename;<\/p>\n<p>public static $objcount = 0; \/\/define a static properties public $total = 0;<\/p>\n<p>public function __construct($eid,$ename)<\/p>\n<p>{<\/p>\n<p>$this-&gt;eid = $eid;<\/p>\n<p>$this-&gt;ename = $ename; self::$objcount++;<\/p>\n<p>}<\/p>\n<p>\/\/eid<\/p>\n<p>public function\u00a0 geteid()<\/p>\n<p>{<\/p>\n<p>return $this-&gt;eid;<\/p>\n<p>}<\/p>\n<p>public function seteid($value)<\/p>\n<p>{<\/p>\n<p>$this-&gt;eid = $value;<\/p>\n<p>}<\/p>\n<p>\/\/ename<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">public function getename()<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0{<\/p>\n<p>return $this-&gt;ename;<\/p>\n<p>}<\/p>\n<p>public function setename($value)<\/p>\n<p>{<\/p>\n<p>$this-&gt;ename = $value;<\/p>\n<p>}<\/p>\n<p>public static function objectcount()<\/p>\n<p>{<\/p>\n<p>return self::$objcount<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>$obj1 = new Employee(1,&#8217;One&#8217;);<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;.Employee::objectcount();<\/p>\n<p>$obj2 = new Employee(2,&#8217;Two&#8217;);<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;.Employee::$objcount ;<\/p>\n<p>$obj3 = new Employee(3,&#8217;Three&#8217;);<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;.Employee::objectcount();<\/p>\n<p>$obj4 = new Employee(4,&#8217;Four&#8217;);<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;.Employee::objectcount();<\/p>\n<p>&nbsp;<\/p>\n<p>Then the output will be:<\/p>\n<p>&nbsp;<\/p>\n<p>1<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<p>4<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Loop through an object\u2019s properties<\/strong><\/p>\n<ul>\n<li>Foreach loop is used to loop through an object\u2019s each properties.<\/li>\n<li>The syntax of foreach loop through an object\u2019s properties<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>foreach ($objectName as [$propertyName =&gt; ] $propertyValue)<\/p>\n<p>{<\/p>\n<p>Statement(s);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li style=\"text-align: justify\">A foreach loop is coded inside a method of an object loops through object\u2019s private, protected and public properties.<\/li>\n<li style=\"text-align: justify\">A foreach loop is coded outside of an object loops through only object\u2019s public properties.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The following example demonstrate the use of foreach loop.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">class Employee<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public $eid,$ename;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">private $edept, $esal;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function __construct($eid,$ename)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$this-&gt;eid = $eid;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$this-&gt;ename = $ename;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/eid<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function geteid()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">return $this-&gt;eid;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function seteid($value)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$this-&gt;eid = $value;<\/span><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/ename<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function getename()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">return $this-&gt;ename;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function setename($value)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$this-&gt;ename = $value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/edat<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function getedept()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">return $this-&gt;edept;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function setedept($value)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$this-&gt;edept = $value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/esal<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function getesal()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">return $this-&gt;esal;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function setesal($value)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$this-&gt;esal = $value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/show<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public function show()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;ul&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">foreach($this as $name =&gt; $value)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;li&gt; $name : $value &#8220;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;\/ul&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$e = new Employee(1,&#8217;One&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$e-&gt;setedept(&#8216;IT&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$e-&gt;setesal(175000);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$e-&gt;show();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Outside class &lt;br&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;ul&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">foreach($e as $k=&gt;$v)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;li&gt; $k : $v &lt;\/li&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;\/ul&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The output of the above program is :<\/p>\n<ul>\n<li>eid : 1<\/li>\n<li>ename : One<\/li>\n<li>edept : IT<\/li>\n<li><span style=\"text-align: initial;font-size: 1em\">esal : 175000<\/span><\/li>\n<\/ul>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Outside class<\/p>\n<ul>\n<li>eid : 1<\/li>\n<li>ename : One<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on OOP \u2013 II in PHP<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/SOMUlzIELBs\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Reference:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP, Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development UsingHTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education<\/p>\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n<p style=\"text-align: justify\">14. https:\/\/docs.oracle.com\/javase\/tutorial\/java\/concepts\/<\/p>\n<p style=\"text-align: justify\">15. http:\/\/www.tutorialspoint.com\/cplusplus\/cpp_object_oriented.htm<\/p>\n<p style=\"text-align: justify\">16. http:\/\/www.tutorialspoint.com\/php\/php_object_oriented.htm<\/p>\n<p style=\"text-align: justify\">17. http:\/\/inchoo.net\/dev-talk\/understanding-phps-oop-basic-terms-explained\/<\/p>\n","protected":false},"author":3,"menu_order":11,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-155","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/155\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/155\/revisions\/484"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/155\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=155"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=155"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}