note 69467 added to language.oop5.interfaces

From: Date: Thu, 07 Sep 2006 01:01:16 +0000
Subject: note 69467 added to language.oop5.interfaces
Groups: php.notes 
Request: Send a blank email to php-notes+get-116792@lists.php.net to get a copy of this message
What is not mentioned in the manual is that you can use "self" to force object hinting on a method of the implementing class: Consider the following interface: <?php interface Comparable {function compare(self $compare);} ?> Which is then implemented: <?php class String implements Comparable { private $string; function __construct($string) {$this->string = $string;} function compare(self $compare) {return $this->string == $compare->string;} } class Integer implements Comparable { private $integer; function __construct($int) {$this->integer = $int;} function compare(self $compare) {return $this->integer == $compare->integer;} } ?> Comparing Integer with String will result in a fatal error, as it is not an instance of the same class: <?php $first_int = new Integer(3); $second_int = new Integer(3); $first_string = new String("foo"); $second_string = new String("bar"); var_dump($first_int->compare($second_int)); // bool(true) var_dump($first_string->compare($second_string)); // bool(false) var_dump($first_string->compare($second_int)); // Fatal Error ?> ---- Server IP: 217.13.201.10 Probable Submitter: 80.129.77.57 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.interfaces.php Edit -- https://master.php.net/note/edit/69467 Del: integrated -- https://master.php.net/note/delete/69467/integrated Del: useless -- https://master.php.net/note/delete/69467/useless Del: bad code -- https://master.php.net/note/delete/69467/bad+code Del: spam -- https://master.php.net/note/delete/69467/spam Del: non-english -- https://master.php.net/note/delete/69467/non-english Del: in docs -- https://master.php.net/note/delete/69467/in+docs Del: other reasons-- https://master.php.net/note/delete/69467 Reject -- https://master.php.net/note/reject/69467 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116792) next »