|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-03-20 00:40 UTC] freebsd at dds dot nl
Description:
------------
Type hinting doesn't prevent you from calling functions of children that are not defined in the interface or (abstract) class. This mean you could call a function of object x, then create a new class (y) that doesn't implement this function, resulting in an error later on.
Basicaly the principle of encapsulation (on class level) isn't enforced.
Reproduce code:
---------------
<?php
interface X {
public function fa();
}
class Y implements X {
public function fa() {}
public function fb() { echo "no error / warning?"; }
}
function f(X $y) {
$y->fb();
}
f(new Y());
?>
Expected result:
----------------
An error (or at least a warning).
Actual result:
--------------
n/a
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 13:00:02 2025 UTC |
From reading the code the compile or interpater knows or could know that $y is of type X. It can not be something else. It could therefore check if the functions that are used in that functions are defined The reason I request a warning is to prevent errors later. These error could be accuring years later. To solve the error one need to read the code in the class where the error happens. Then decides how to solve this. Options are: to include the function defintion in the interface, define a new interface or just define the function in the class. All these option involve breaking code or setting oneself up to repeat history. Suppose I add a new class years later. I then would read the interface I'm going to implement. So I have: class Z implements X { punction function fa() {} } But when I use it it complains it wants fb(); I then read the interface again. Why does php have interfaces?