php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66781 Errorneous "Cannot make non static method static in" for private methods
Submitted: 2014-02-26 16:58 UTC Modified: 2014-02-27 17:12 UTC
From: maciej dot sz at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.5.9 OS: irrelevant
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: maciej dot sz at gmail dot com
New email:
PHP Version: OS:

 

 [2014-02-26 16:58 UTC] maciej dot sz at gmail dot com
Description:
------------
Private methods are owned by the classes in which they are used, so there should be no problem with the following code:

Test script:
---------------
class Foo {
    private function fooMethod(){}
}

class Bar extends Foo {
    private static function fooMethod(){}
}


Expected result:
----------------
Sounds like the Bar::fooMethod() tries to override the Foo::fooMethod(), even though the Foo::fooMethod() is not a part of the Bar class. They should not affect each other due to private visibility.

Actual result:
--------------
The test code produces:

Fatal error: Cannot make non static method Foo::fooMethod() static in class Bar in /home/www/workspace/tests/static_private_override.php on line 9

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-02-27 17:12 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2014-02-27 17:12 UTC] krakjoe@php.net
That would break anything dependent on Foo calling fooMethod in the object context.

It's desired behaviour.
 [2014-02-27 19:52 UTC] maciej dot sz at gmail dot com
Would break what exactly? Can you give an example? It's private scope we're talking about. C#, Java and C++ don't complain, only PHP does. Here are some test apps if you like:

test.cpp:
class Foo
{
    private: void fooMethod(){}
};

class Bar : Foo {
    private: static void fooMethod(){}
};

int main()
{ return 0; }



Program.cs:
using System;

namespace MonoTest
{
	class Foo
	{
		private void FooMethod(){}
	}

	class Bar : Foo
	{
		private static void FooMethod(){}
	}

	class MainClass
	{
		public static void Main (string[] args)
		{
			var b = new Bar ();
		}
	}
}




Foo.java:
public class Foo {
    private void foo(){}
}

Bar.java:
public class Bar extends Foo {
    public static void foo(){}

    public static void main(String [] args){
        System.out.println("I'm in bar");
    }
}
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 06 20:01:35 2025 UTC