Presentation is loading. Please wait.

Presentation is loading. Please wait.

21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.

Similar presentations


Presentation on theme: "21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared."— Presentation transcript:

1 21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared using the keyword var  Can be initialized when declared To create an instance of a class, use new followed by the class name  Returns a reference to the instance Use the selector -> to access a property of an instance  The property name here does not begin with $  For variable interpolation in a double-quoted string, box the selector expression within {…}s

2 class Simple1 { var $x = 1, $y = 2; } $s = new Simple1; echo " {$s->x} "; echo " {$s->y} "; Outputs 1 2

3 Methods are defined as normal functions within the class definition A method is invoked by an instance using the selector ->, e.g., $s->sum(6) Pseudo-variable $this is used in method definitions to reference the calling object (the current instance)

4 Several functions whose names begin with double underscore are “magical” in PHP classes  Can’t have functions with these names in a classes unless you want the magic functionality associated with them __construct(), if defined, is called when the class is instantiated to allow instances to be initialized  Commonly pass initialization parameters __destruct(), if defined, is called when the object becomes eligible for garbage collection __toString() returns the string that provides the value of an instance when it appears where a string is expected (conversion to string)

5 class Simple2 { var $x, $y; function __construct($first, $second) { $this->x = $first; $this->y = $second; } function __toString() { return strval($this->x); } function sum( $val ) { return $this->x + $this->y + $val; } } Continued

6 $s = new Simple2(2, 4); echo " {$s} "; echo " {$s->y} "; echo " {$s->sum(6)} "; Outputs 2 4 12

7 Access Modifiers Access modifiers control the visibility of properties and methods  Placed in front of property and method declarations public (the default): accessible from inside or outside the class private : accessible only from inside the class protected : wait until we get to inheritance

8 Accessor Functions The values of private properties of instances can be accessed if we define the magical method __get() A minimal implementation is function __get($prop) { return $this->$prop; // Note the '$' on '$prop' }

9 class Simple3 { private $x, $y; public function __construct($first, $second) { $this->x = $first; $this->y = $second; } public function __toString() { return strval($this->x); } public function __get($prop) { return $this->$prop; } } Continued

10 $s = new Simple3(2, 4); echo " {$s} "; echo " {$s->y} "; Outputs 2 4 When $s->y is executed, __get() is called and passed y The body of __get() can modify the value before it is returned— e.g., return abs($this->$prop);

11 The value of a private property can be updated if we define the magical method __set() A minimal implementation is function __set($prop, $val) { $this->$prop = $val; } Suppose this is inserted in the above code, and we add $s = new Simple3(2, 4); $s->y += 10; echo " {$s->y} ";  Outputs 14 When $s->y += 10; is execute, __set() is called and passed y and 10

12 The body of __set() could change the value that is written to the property—e.g., if ( $val > 100 ) $val = 100; $this->$prop = $val;

13 Inheritance Use the extends keyword to indicate that a class is a subclass of another Properties and methods are inherited unless they’re overridden To invoke a method of the parent that’s overridden in the current class, precede the method name with parent:: —e.g., parent::foo(2)  The parent’s constructor is not automatically called—must use parent::__construct(…);  The same applies to destructors Access modifier protected makes the property or method invisible outside the class  But (unlike private ) allows it to be inherited

14 class A { protected $x; protected $y = 5; function __construct($a) { $this->x = $a; } public function foo() { return $this->x + $this->y; } public function bar() { return $this->x - $this->y; } } Continued

15 class B extends A { protected $y = 10; public function __construct($a) { parent::__construct($a); } public function bar() { return $this->x * $this->y; } public function xan() { return $this->x / $this->y; } } Continued

16 $c1 = new A(1); echo " {$c1->foo()} "; echo " {$c1->bar()} "; $c2 = new B(2); echo " {$c2->foo()} "; echo " {$c2->bar()} "; echo " {$c2->xan()} "; Outputs 6 -4 12 20 0.2

17 Preventing Inheritance and Overriding with final If a function declaration is preceded by keyword final, it can’t be overridden in subclasses Preceding a class definition with final prevents it from being subclassed

18 Abstract Methods and Classes Declare an abstract method by preceding its signature (with no implementation) with keyword abstract Declare an abstract class by preceding its definition with keyword abstract Can’t create an instance of an abstract class Any class that contains at least 1 abstract method must also be abstract When inheriting from an abstract class, all abstract methods of the parent must be defined by the child  These methods must be defined with the same or a less restricted visibility  E.g., if the abstract method is defined as protected, the implementation must be protected or public, but not private

19 abstract class A { protected $x; protected $y; public function foo() { return $this->x + $this->y; } abstract protected function bar(); }

20 class B extends A { public function __construct($a, $b) { $this->x = $a; $this->y = $b; } public function bar() { return $this->x - $this->y; } } $c1 = new B(1, 2); echo " {$c1->foo()} "; echo " {$c1->bar()} "; Outputs 3

21 Implementing Interfaces Interfaces are seen as workarounds for multiple inheritance (PHP allows only single inheritance) They’re similar to interfaces supported by other OO languages (e.g., Java) An interface specifies a set of functions (giving just their signatures) that must be implemented in classes that implement the interface A class can inherit from one class and implement 1 or more interfaces

22 interface A { public function foo($a); } class B { private $x=10; public function bar() { return $this->x; } } Continued

23 class C extends B implements A { public function foo($a) { return $a; } } $c1 = new C; echo " {$c1->foo(8)} "; echo " {$c1->bar()} "; Outputs 8 10

24 Usefulness for Generating HTML File cl8a.inc <?php class A { public function greeting() { echo " Hello, user! "; } public function intro() { echo " This is an experimental website. "; } } ?>

25 File cl8b.inc <?php class B extends A { public function intro() { echo " This is a student website. "; } public function signoff() { echo " Good bye! "; } } ?>

26 File cl8.php <?php include('cl8a.inc'); include('cl8b.inc'); $page = new B; $page->greeting(); $page->intro(); $page->signoff(); ?>


Download ppt "21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared."

Similar presentations


Ads by Google