Object Oriented programming Chapter 4 1 Monica Deshmane(Haribhai V. Desai College,Pune)
What we learn? • Serialization • Inheritance • Interfaces • Encapsulation 2 Monica Deshmane(Haribhai V. Desai College,Pune)
• Serialization Monica Deshmane(Haribhai V. Desai College,Pune) 3
• the serialize() function converts a storable representation of a value. • To serialize data means to convert a value to a sequence of bits, so that it can be stored in a file, a memory buffer, or transmitted across a network. • String serialize(value); • Value- Specifies the value to be serialized Monica Deshmane(Haribhai V. Desai College,Pune) 4
• When serializing object php calls __sleep() before serialization. • This allows ex. Clean up before serialize. • When unserializing object php calls __wakeup() before unserialization. • This allows to convert string of byte stream which returned as value which can be stored anywhere. Monica Deshmane(Haribhai V. Desai College,Pune) 5
• <?php // classa.inc: class A { public $one = 1; public function show_one() { echo $this->one; } } // page1.php: include("classa.inc"); Monica Deshmane(Haribhai V. Desai College,Pune) 6
$a = new A; $s = serialize($a); // store $s somewhere where page2.php can find it. file_put_contents('store', $s); // page2.php: // this is needed for the unserialize to work properly. include("classa.inc"); $s = file_get_contents('store'); $a = unserialize($s); // now use the function show_one() of the $a object. $a->show_one(); ?> Monica Deshmane(Haribhai V. Desai College,Pune) 7
• Inheritance Monica Deshmane(Haribhai V. Desai College,Pune) 8
Monica Deshmane(Haribhai V. Desai College,Pune) 9 • Inheritance in OOP = When a class derives from another class. • The child class will inherit all the public and protected properties and methods from the parent class. • In addition, it can have its own properties and methods. • An inherited class is defined by using the extends keyword.
Example2- <?php class A { public function f1($string) { echo ”in f1”.$string; } public function f2() { echo ”in f2”; } } class B extends A { public function f1($string) { echo “in B f1”.$string; } } $a = new A(); $b = new B(); $a->f1(‘hi'); // Output: in f1hi' $b->f1(‘hi'); // Output: in B f1hi' ?> Monica Deshmane(Haribhai V. Desai College,Pune) 10
Scope resolution(::)operator • The Scope Resolution Operator (also called the double colon)is a token that allows access to static, constant, and overridden properties or methods of a class. • When referencing these items from outside the class definition, use the name of the class. • As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static). Monica Deshmane(Haribhai V. Desai College,Pune) 11
example • <?php class MyClass { const CONST_VALUE = 'A constant value'; } $classname = 'MyClass'; echo $classname::CONST_VALUE; // As of PHP 5.3.0 echo MyClass::CONST_VALUE; ?> Monica Deshmane(Haribhai V. Desai College,Pune) 12
Parent, self Monica Deshmane(Haribhai V. Desai College,Pune) 13 Three special keywords self, parent and static are used to access properties or methods from inside the class definition. Example #2 :: from inside the class definition <?php class OtherClass extends MyClass { public static $my_static = 'static var'; public static function doubleColon() { echo parent::CONST_VALUE . "n"; echo self::$my_static . "n"; } } $classname = 'OtherClass'; echo $classname::doubleColon(); // As of PHP 5.3.0 OtherClass::doubleColon(); ?>
Access specifiers: public • <?php class Fruit { public $name; function set_name($n) { // a public function (default) $this->name = $n; } } $mango = new Fruit(); $mango->set_name('Mango'); Monica Deshmane(Haribhai V. Desai College,Pune) 14
public • Any one can access • Through out script accessible • By default public Monica Deshmane(Haribhai V. Desai College,Pune) 15
Access specifiers: private • <?php class Fruit { public $weight; private function set_weight($n) { // a private function $this->weight = $n; } } $mango = new Fruit(); $mango->set_weight('300'); // ERROR ?> Monica Deshmane(Haribhai V. Desai College,Pune) 16
Private: • Used for Information hiding • Accessible within class only • Restrict objects from accessing class members Monica Deshmane(Haribhai V. Desai College,Pune) 17
Access specifiers: protected • <?php class Fruit { public $color; protected function set_color($n) { // a protected function $this->color = $n; } $mango = new Fruit(); $mango->set_color('Yellow'); // ERROR ?> Monica Deshmane(Haribhai V. Desai College,Pune) 18
protected • Like private • Accessible within class • And also in derived class Monica Deshmane(Haribhai V. Desai College,Pune) 19
Final keyword- • Final method- • <?php class parentClass { final private function someMethod() { } } class childClass extends parentClass { private function someMethod() { } } ?> -error "Fatal error Such behaviour looks slight unexpected because in child class we cannot know, which private methods exists in a parent class and vice versa. Monica Deshmane(Haribhai V. Desai College,Pune) 20
Final class • Final class A { } Class B extends class A { } //fatal error: can not inherit class Monica Deshmane(Haribhai V. Desai College,Pune) 21
• The use of final keyword is just like that occurs in Java • In java final has three uses 1) prevent class Inheritance • 2) prevent method overriding or redifination of method in subclass • 3) and to declare constants But the third point seems to be missing from the PHP Monica Deshmane(Haribhai V. Desai College,Pune) 22
Abstract class methods • Can not create object An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code. An abstract class or method is defined with the abstract keyword: • <?php abstract class ParentClass { abstract public function Method1(); abstract public function Method2($name, $color); abstract public function Method3() : string; } ?> Monica Deshmane(Haribhai V. Desai College,Pune) 23
Static • Static keyword used • Without instanciation we can access properties & members of class. • Static used after visibility or access modefiers declaration. • Ex. Public static $a; Monica Deshmane(Haribhai V. Desai College,Pune) 24
• 4.6 Interfaces Monica Deshmane(Haribhai V. Desai College,Pune) 25
interface shape <?php interface myinterface { public function area(); public function volume(); } class cylinder implements myinterface { var $pi=3.14; var $r=3; var $h=3; Monica Deshmane(Haribhai V. Desai College,Pune) 26
public function area() { $Area=2*$this->pi*$this->r*$this->h; echo"Area of cylinder is $Area<br>"; } public function volume() { $volume=$this->pi*$this->r*$this->r*$this->h; echo"volume of cylinder is $volume<br>"; } } $c=new cylinder(); $c->area(); $c->volume(); ?> Monica Deshmane(Haribhai V. Desai College,Pune) 27
Monica Deshmane(Haribhai V. Desai College,Pune) 28 Abstract class 1Abstract function & other defined extends Not pure Abstract class A { Public $a;// allowed property Abstract function f(); …. } Class B extends A{} Interface All are abstract Implements Pure abstract class Inteface A { //not allowed } Class A implements A
• 4.7Encapsulation Monica Deshmane(Haribhai V. Desai College,Pune) 29
Encapsulation • Encapsulation can be used if the properties of the object are private and updating them through public methods. • It is binding datamembers with member functions. • Encapsulation in PHP can be achieved using the implementation of access specifiers. • __get() and __set() used Monica Deshmane(Haribhai V. Desai College,Pune) 30

Chap4 oop class (php) part 2

  • 1.
    Object Oriented programming Chapter4 1 Monica Deshmane(Haribhai V. Desai College,Pune)
  • 2.
    What we learn? •Serialization • Inheritance • Interfaces • Encapsulation 2 Monica Deshmane(Haribhai V. Desai College,Pune)
  • 3.
  • 4.
    • the serialize()function converts a storable representation of a value. • To serialize data means to convert a value to a sequence of bits, so that it can be stored in a file, a memory buffer, or transmitted across a network. • String serialize(value); • Value- Specifies the value to be serialized Monica Deshmane(Haribhai V. Desai College,Pune) 4
  • 5.
    • When serializingobject php calls __sleep() before serialization. • This allows ex. Clean up before serialize. • When unserializing object php calls __wakeup() before unserialization. • This allows to convert string of byte stream which returned as value which can be stored anywhere. Monica Deshmane(Haribhai V. Desai College,Pune) 5
  • 6.
    • <?php // classa.inc: classA { public $one = 1; public function show_one() { echo $this->one; } } // page1.php: include("classa.inc"); Monica Deshmane(Haribhai V. Desai College,Pune) 6
  • 7.
    $a = newA; $s = serialize($a); // store $s somewhere where page2.php can find it. file_put_contents('store', $s); // page2.php: // this is needed for the unserialize to work properly. include("classa.inc"); $s = file_get_contents('store'); $a = unserialize($s); // now use the function show_one() of the $a object. $a->show_one(); ?> Monica Deshmane(Haribhai V. Desai College,Pune) 7
  • 8.
  • 9.
    Monica Deshmane(Haribhai V.Desai College,Pune) 9 • Inheritance in OOP = When a class derives from another class. • The child class will inherit all the public and protected properties and methods from the parent class. • In addition, it can have its own properties and methods. • An inherited class is defined by using the extends keyword.
  • 10.
    Example2- <?php class A { public functionf1($string) { echo ”in f1”.$string; } public function f2() { echo ”in f2”; } } class B extends A { public function f1($string) { echo “in B f1”.$string; } } $a = new A(); $b = new B(); $a->f1(‘hi'); // Output: in f1hi' $b->f1(‘hi'); // Output: in B f1hi' ?> Monica Deshmane(Haribhai V. Desai College,Pune) 10
  • 11.
    Scope resolution(::)operator • TheScope Resolution Operator (also called the double colon)is a token that allows access to static, constant, and overridden properties or methods of a class. • When referencing these items from outside the class definition, use the name of the class. • As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static). Monica Deshmane(Haribhai V. Desai College,Pune) 11
  • 12.
    example • <?php class MyClass{ const CONST_VALUE = 'A constant value'; } $classname = 'MyClass'; echo $classname::CONST_VALUE; // As of PHP 5.3.0 echo MyClass::CONST_VALUE; ?> Monica Deshmane(Haribhai V. Desai College,Pune) 12
  • 13.
    Parent, self Monica Deshmane(HaribhaiV. Desai College,Pune) 13 Three special keywords self, parent and static are used to access properties or methods from inside the class definition. Example #2 :: from inside the class definition <?php class OtherClass extends MyClass { public static $my_static = 'static var'; public static function doubleColon() { echo parent::CONST_VALUE . "n"; echo self::$my_static . "n"; } } $classname = 'OtherClass'; echo $classname::doubleColon(); // As of PHP 5.3.0 OtherClass::doubleColon(); ?>
  • 14.
    Access specifiers: public •<?php class Fruit { public $name; function set_name($n) { // a public function (default) $this->name = $n; } } $mango = new Fruit(); $mango->set_name('Mango'); Monica Deshmane(Haribhai V. Desai College,Pune) 14
  • 15.
    public • Any onecan access • Through out script accessible • By default public Monica Deshmane(Haribhai V. Desai College,Pune) 15
  • 16.
    Access specifiers: private •<?php class Fruit { public $weight; private function set_weight($n) { // a private function $this->weight = $n; } } $mango = new Fruit(); $mango->set_weight('300'); // ERROR ?> Monica Deshmane(Haribhai V. Desai College,Pune) 16
  • 17.
    Private: • Used forInformation hiding • Accessible within class only • Restrict objects from accessing class members Monica Deshmane(Haribhai V. Desai College,Pune) 17
  • 18.
    Access specifiers: protected •<?php class Fruit { public $color; protected function set_color($n) { // a protected function $this->color = $n; } $mango = new Fruit(); $mango->set_color('Yellow'); // ERROR ?> Monica Deshmane(Haribhai V. Desai College,Pune) 18
  • 19.
    protected • Like private •Accessible within class • And also in derived class Monica Deshmane(Haribhai V. Desai College,Pune) 19
  • 20.
    Final keyword- • Finalmethod- • <?php class parentClass { final private function someMethod() { } } class childClass extends parentClass { private function someMethod() { } } ?> -error "Fatal error Such behaviour looks slight unexpected because in child class we cannot know, which private methods exists in a parent class and vice versa. Monica Deshmane(Haribhai V. Desai College,Pune) 20
  • 21.
    Final class • Finalclass A { } Class B extends class A { } //fatal error: can not inherit class Monica Deshmane(Haribhai V. Desai College,Pune) 21
  • 22.
    • The useof final keyword is just like that occurs in Java • In java final has three uses 1) prevent class Inheritance • 2) prevent method overriding or redifination of method in subclass • 3) and to declare constants But the third point seems to be missing from the PHP Monica Deshmane(Haribhai V. Desai College,Pune) 22
  • 23.
    Abstract class methods •Can not create object An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code. An abstract class or method is defined with the abstract keyword: • <?php abstract class ParentClass { abstract public function Method1(); abstract public function Method2($name, $color); abstract public function Method3() : string; } ?> Monica Deshmane(Haribhai V. Desai College,Pune) 23
  • 24.
    Static • Static keywordused • Without instanciation we can access properties & members of class. • Static used after visibility or access modefiers declaration. • Ex. Public static $a; Monica Deshmane(Haribhai V. Desai College,Pune) 24
  • 25.
    • 4.6 Interfaces MonicaDeshmane(Haribhai V. Desai College,Pune) 25
  • 26.
    interface shape <?php interface myinterface { publicfunction area(); public function volume(); } class cylinder implements myinterface { var $pi=3.14; var $r=3; var $h=3; Monica Deshmane(Haribhai V. Desai College,Pune) 26
  • 27.
    public function area() { $Area=2*$this->pi*$this->r*$this->h; echo"Areaof cylinder is $Area<br>"; } public function volume() { $volume=$this->pi*$this->r*$this->r*$this->h; echo"volume of cylinder is $volume<br>"; } } $c=new cylinder(); $c->area(); $c->volume(); ?> Monica Deshmane(Haribhai V. Desai College,Pune) 27
  • 28.
    Monica Deshmane(Haribhai V.Desai College,Pune) 28 Abstract class 1Abstract function & other defined extends Not pure Abstract class A { Public $a;// allowed property Abstract function f(); …. } Class B extends A{} Interface All are abstract Implements Pure abstract class Inteface A { //not allowed } Class A implements A
  • 29.
  • 30.
    Encapsulation • Encapsulation canbe used if the properties of the object are private and updating them through public methods. • It is binding datamembers with member functions. • Encapsulation in PHP can be achieved using the implementation of access specifiers. • __get() and __set() used Monica Deshmane(Haribhai V. Desai College,Pune) 30