21. php classes to define a class, use the keyword class followed by the name and a block with the...
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)TRANSCRIPT
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
class Simple1{ var $x = 1, $y = 2;}
$s = new Simple1;echo "<p>{$s->x}</p>";echo "<p>{$s->y}</p>";
Outputs12
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)
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)
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
$s = new Simple2(2, 4);echo "<p>{$s}</p>";echo "<p>{$s->y}</p>";echo "<p>{$s->sum(6)}</p>";
Outputs 2412
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
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'}
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
$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);
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
The body of __set() could change the value that is written to the property—e.g.,
if ( $val > 100 ) $val = 100;$this->$prop = $val;
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
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
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
$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
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
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
abstract class A{ protected $x; protected $y;
public function foo() { return $this->x + $this->y; }
abstract protected function bar();}
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
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
interface A{ public function foo($a);}
class B{ private $x=10;
public function bar() { return $this->x; }}
Continued
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
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>"; }}?>
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>"; }}?>
File cl8.php
<html><body><?php include('cl8a.inc'); include('cl8b.inc'); $page = new B; $page->greeting(); $page->intro(); $page->signoff();?></body></html>