php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43914 Invalid invocation of non-static class function
Submitted: 2008-01-23 00:34 UTC Modified: 2008-01-28 23:31 UTC
From: mo at modejong dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: WinXP
Private report: No CVE-ID: None
 [2008-01-23 00:34 UTC] mo at modejong dot com
Description:
------------
PHP allows invocation of an instance method in a class that
is unrelated to the current class. I would think that the
correct thing to do in this case is to generate an error
at runtime when this specific case is detected.

Reproduce code:
---------------
class One {
  function foo() {
    $isnull = ($this == NULL) ? "TRUE" : "FALSE";
    $instanceof = ($this instanceof One) ? "TRUE" : "FALSE";
    print "One::foo is \$this null ($isnull) : is instanceof One ($instanceof)\n";
  }
}

class Two {
  function foo() {
    $isnull = ($this == NULL) ? "TRUE" : "FALSE";
    $instanceof = ($this instanceof Two) ? "TRUE" : "FALSE";
    print "Two::foo is \$this null ($isnull) : is instanceof Two ($instanceof)\n";
    return One::foo();
  }
}

$obj = new Two;
$obj->foo();


Expected result:
----------------
When executed, PHP prints:

Two::foo is $this null (FALSE) : is instanceof Two (TRUE)
One::foo is $this null (FALSE) : is instanceof One (FALSE)

It should generate an error here since Two is unrelated
to class One. This check should be done when invoking
the instance method, if the $this ref is not NULL then
the type of $this should be checked before the instance
method can be invoked in One.

I already read bug 42559, it seems to have been rejected
as "not a bug", but this issue is clearly a bug. Use of
a method call like CLASS::METHOD inside a class def does
not mean the method being invoked is static.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-28 23:31 UTC] tony2001@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

>Use of a method call like CLASS::METHOD inside a class def does
>not mean the method being invoked is static.

Yes, it does.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 08:01:30 2024 UTC