php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51524 Class static method confusion
Submitted: 2010-04-10 03:37 UTC Modified: 2010-04-12 11:48 UTC
From: david71rj at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.2 OS: Windows 7/64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: david71rj at gmail dot com
New email:
PHP Version: OS:

 

 [2010-04-10 03:37 UTC] david71rj at gmail dot com
Description:
------------
If I use public method as static in a method of a second function, this send $this 
from second class, don't of the first.

Test script:
---------------
http://codepad.org/8hW4Qtbo

Expected result:
----------------
array
  0 => string 'static' (length=6)
  1 => int 1
array
  0 => string 'object' (length=6)
  1 => 
    object(test)[2]
  2 => int 1
array
  0 => string 'static' (length=6)
  1 => int 1
array
  0 => string 'object' (length=6)
  1 => 
    object(test)[3] <<<<<<<<<<<<<<< OKAY
  2 => int 1

Actual result:
--------------
array
  0 => string 'static' (length=6)
  1 => int 1
array
  0 => string 'object' (length=6)
  1 => 
    object(test)[2]
  2 => int 1
array
  0 => string 'static' (length=6)
  1 => int 1
array
  0 => string 'object' (length=6)
  1 => 
    object(test2)[3] <<<<<<<<< WHY "test2" IF DON'T?
  2 => int 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-10 17:49 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-04-10 17:49 UTC] johannes@php.net
If a static method is called from an object context the callers $this is sent. This i due to compatibility with the way PHP 4 worked.
 [2010-04-10 18:06 UTC] pajoye@php.net
-Status: Bogus +Status: Open
 [2010-04-10 18:06 UTC] pajoye@php.net
Not sure you can simply bogus it "because that's the way php4 works" :)

Should we not fix it in trunk instead? Or at least leave that request open until we have a decision.
 [2010-04-10 19:27 UTC] david71rj at gmail dot com
Not make sense, a call static is call static, don't can exist $this. In 
counterpart, if the user need pass a object reference, he must be to specific 
how a parameter.

public function x($object = null){
  if($object
  && get_class($object) == 'B') {
    // Now make sense, but not is $this
  }
}

Bye.
 [2010-04-12 11:48 UTC] pajoye@php.net
-Status: Open +Status: Bogus
 [2010-04-12 11:48 UTC] pajoye@php.net
My comment was supposed to land in #51527, this one is obviously not a bug (bogus).
 [2010-04-12 19:04 UTC] david71rj at gmail dot com
But how this not is a bug?

What I don't understand is: if I call a method as static, $this cannot exists, one 
time that $this, for me is 'object of same class' and not 'object of any class'.

As example, how useful it (use $this as secondary object) can be?

Bye.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 10:01:32 2025 UTC