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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 02 07:01:30 2024 UTC