3 Replies - 1370 Views - Last Post: 14 September 2011 - 11:28 PM Rate Topic: -----

#1 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Is this a bug in PHP?

Posted 14 September 2011 - 11:07 PM

I just spent about 2 hours debugging this issue (v 5.3.0 if that helps). Run this code, and you'll see what I mean:
<?php
class ABC
{
	public static function ABC()
	{
		echo 'Testing';
	}
}
ABC::ABC();
?>


This code generated the error "Constructor ABC::ABC() cannot be static in testing.php on line 8". After pulling out what little hair I have left (I swear I've grown 50% balder from programming), I finally figure out it is because I have a public static function that has an identical name to the class. It seems that if you have a class called PurpleMonkeyDishwasher, then you can't call a static method named PurpleMonkeyDishwasher, because PHP interprets that method as being the constructor.

Is there a reason for this, or is this a genuine bug I should be reporting to the PHP crew?

Is This A Good Question/Topic? 0
  • +

Replies To: Is this a bug in PHP?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,317
  • Joined: 08-June 10

Re: Is this a bug in PHP?

Posted 14 September 2011 - 11:18 PM

indeed, if you don’t have a (PHP 5) constructor then the method with the same name as the class is used instead. and since constructor methods can’t be static, it gives an error. this behaviour was used in PHP 4 to define the class constructor and is left in PHP 5 for compatibility reasons (like the var keyword to define properties).

This post has been edited by Dormilich: 14 September 2011 - 11:19 PM

Was This Post Helpful? 3
  • +
  • -

#3 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Is this a bug in PHP?

Posted 14 September 2011 - 11:21 PM

Thanks for clearing that up. It's a hell of a thing to trip over when you're coding, especially since it's legacy practice. I've only been coding in PHP for the past 3 years, so I started roughly when 5.3 came out, I had no idea this was the case in 4.x
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,317
  • Joined: 08-June 10

Re: Is this a bug in PHP?

Posted 14 September 2011 - 11:28 PM

well, I started somewhere around 4.1 and I still can remember the pain rewriting my PHP 4 scripts/classes into PHP 5.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1