26
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

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

Embed Size (px)

DESCRIPTION

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)

Citation preview

Page 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

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

Page 2: 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

class Simple1{ var $x = 1, $y = 2;}

$s = new Simple1;echo "<p>{$s->x}</p>";echo "<p>{$s->y}</p>";

Outputs12

Page 3: 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

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)

Page 4: 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

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)

Page 5: 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

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

Page 6: 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

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

Outputs 2412

Page 7: 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

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

Page 8: 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

Accessor Functions The values of private properties of instances can be accessed if

we define the magical method __get()

A minimal implementation isfunction __get($prop){ return $this->$prop; // Note the '$' on '$prop'}

Page 9: 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

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

Page 10: 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

$s = new Simple3(2, 4);echo "<p>{$s}</p>";echo "<p>{$s->y}</p>";

Outputs24

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);

Page 11: 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

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 "<p>{$s->y}</p>"; Outputs 14

When $s->y += 10;

is execute, __set() is called and passed y and 10

Page 12: 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

The body of __set() could change the value that is written to the property—e.g.,

if ( $val > 100 ) $val = 100;$this->$prop = $val;

Page 13: 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

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

Page 14: 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

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

Page 15: 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

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

Page 16: 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

$c1 = new A(1);echo "<p>{$c1->foo()}</p>";echo "<p>{$c1->bar()}</p>";$c2 = new B(2);echo "<p>{$c2->foo()}</p>";echo "<p>{$c2->bar()}</p>";echo "<p>{$c2->xan()}</p>";

Outputs6-412200.2

Page 17: 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

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

Page 18: 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

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

Page 19: 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

abstract class A{ protected $x; protected $y;

public function foo() { return $this->x + $this->y; }

abstract protected function bar();}

Page 20: 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

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 "<p>{$c1->foo()}</p>";echo "<p>{$c1->bar()}</p>";

Outputs 3-1

Page 21: 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

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

Page 22: 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

interface A{ public function foo($a);}

class B{ private $x=10;

public function bar() { return $this->x; }}

Continued

Page 23: 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

class C extends B implements A{ public function foo($a) { return $a; }}

$c1 = new C;echo "<p>{$c1->foo(8)}</p>";echo "<p>{$c1->bar()}</p>";

Outputs810

Page 24: 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

Usefulness for Generating HTML File cl8a.inc

<?phpclass A{ public function greeting() { echo "<p>Hello, user!</p>"; }

public function intro() { echo "<p>This is an experimental website.</p>"; }}?>

Page 25: 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

File cl8b.inc

<?phpclass B extends A{ public function intro() { echo "<p>This is a student website.</p>"; }

public function signoff() { echo "<p>Good bye!</p>"; }}?>

Page 26: 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

File cl8.php

<html><body><?php include('cl8a.inc'); include('cl8b.inc'); $page = new B; $page->greeting(); $page->intro(); $page->signoff();?></body></html>