php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32379 type hinting doesn't inforce encapsulation
Submitted: 2005-03-20 00:40 UTC Modified: 2005-05-29 18:10 UTC
From: freebsd at dds dot nl Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: freebsd at dds dot nl
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-20 00:55 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Type hints don\'t check what you are doing with your object, 
they just help you to know what you have... 
 [2005-03-20 23:03 UTC] freebsd at dds dot nl
If this isn't a bug I would like it to become a feature.
 [2005-03-20 23:05 UTC] freebsd at dds dot nl
My email is wrong it should be freebsd at akruijff dot nl. It seems I can nott change this.
 [2005-03-20 23:21 UTC] helly@php.net
PHP is loosely typed. What you request would change the nature of PHP completley. Thus this will never happen.
 [2005-05-29 18:10 UTC] freebsd at dds dot nl
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?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 10:01:31 2024 UTC