Consider the following code:
foo.php:
<?php
class test {
public static function foo() { echo "I'm foo in class test\n"; }
public static function foo2() { self::foo(); }
}
?>
foo2.php:
<?php
namespace test;
function foo() { echo "I'm foo in namespace test\n"; }
?>
test.php:
<?php
include 'foo.php';
include 'foo2.php';
test::foo(); // I'm foo in namespace test
use test::foo as dummy;
test::foo(); // I'm foo in namespace test
test::foo2(); // I'm foo in class test
$test = 'test';
$test::foo(); // I'm foo in class test
call_user_func(array('test', 'foo')); // I'm foo in class test
?>
Please review the following observations:
There's a name clash that goes undetected: test::foo refers to both a
namespaced function and a static method.
Once the clash occur there's no way to refer to the static method through a
static reference, except when within the class scope where you can refer to
the method through self::
The static method remains partially hidden by the namespaced function.
Best Regards,
Martin Alterisio
Martin Alterisio wrote:
Consider the following code:
foo.php:
<?php
class test {
public static function foo() { echo "I'm foo in class test\n"; }
public static function foo2() { self::foo(); }
}
?>foo2.php:
<?php
namespace test;
function foo() { echo "I'm foo in namespace test\n"; }
?>test.php:
<?php
include 'foo.php';
include 'foo2.php';
test::foo(); // I'm foo in namespace test
use test::foo as dummy;
test::foo(); // I'm foo in namespace test
test::foo2(); // I'm foo in class test
$test = 'test';
$test::foo(); // I'm foo in class test
call_user_func(array('test', 'foo')); // I'm foo in class test
?>Please review the following observations:
There's a name clash that goes undetected: test::foo refers to both a
namespaced function and a static method.Once the clash occur there's no way to refer to the static method through a
static reference, except when within the class scope where you can refer to
the method through self::
The static method remains partially hidden by the namespaced function.
Don't forget about ::test::foo() which refers to class test, method
foo(). However, this is an issue, and one of the main reasons I dislike
putting functions and constants in namespaces, as this ends up sort of
like OO without inheritance and confuses the issue of static methods as
you pointed out.
However, having said that, in my experience, developers either use
functions or OO, very rarely mixing the two on an extensive basis, and
so name collisions become much less likely between static methods and
namespaced functions.
Greg
Greg Beaver schreef:
Martin Alterisio wrote:
Consider the following code:
foo.php:
<?php
class test {
public static function foo() { echo "I'm foo in class test\n"; }
public static function foo2() { self::foo(); }
}
?>foo2.php:
<?php
namespace test;
function foo() { echo "I'm foo in namespace test\n"; }
?>test.php:
<?php
include 'foo.php';
include 'foo2.php';
test::foo(); // I'm foo in namespace test
use test::foo as dummy;
test::foo(); // I'm foo in namespace test
test::foo2(); // I'm foo in class test
$test = 'test';
$test::foo(); // I'm foo in class test
call_user_func(array('test', 'foo')); // I'm foo in class test
?>Please review the following observations:
There's a name clash that goes undetected: test::foo refers to both a
namespaced function and a static method.Once the clash occur there's no way to refer to the static method through a
static reference, except when within the class scope where you can refer to
the method through self::
The static method remains partially hidden by the namespaced function.Don't forget about ::test::foo() which refers to class test, method
foo(). However, this is an issue, and one of the main reasons I dislike
putting functions and constants in namespaces, as this ends up sort of
like OO without inheritance and confuses the issue of static methods as
you pointed out.However, having said that, in my experience, developers either use
functions or OO, very rarely mixing the two on an extensive basis, and
so name collisions become much less likely between static methods and
namespaced functions.
why exactly should we need to have this ambiguity and possible naming collision?
I thought namespaces are about avoiding naming collisions?
Greg
2007/12/21, Greg Beaver cellog@php.net:
Martin Alterisio wrote:
Consider the following code:
foo.php:
<?php
class test {
public static function foo() { echo "I'm foo in class test\n"; }
public static function foo2() { self::foo(); }
}
?>foo2.php:
<?php
namespace test;
function foo() { echo "I'm foo in namespace test\n"; }
?>test.php:
<?php
include 'foo.php';
include 'foo2.php';
test::foo(); // I'm foo in namespace test
use test::foo as dummy;
test::foo(); // I'm foo in namespace test
test::foo2(); // I'm foo in class test
$test = 'test';
$test::foo(); // I'm foo in class test
call_user_func(array('test', 'foo')); // I'm foo in class test
?>Please review the following observations:
There's a name clash that goes undetected: test::foo refers to both a
namespaced function and a static method.Once the clash occur there's no way to refer to the static method
through a
static reference, except when within the class scope where you can refer
to
the method through self::
The static method remains partially hidden by the namespaced function.Don't forget about ::test::foo() which refers to class test, method
foo(). However, this is an issue, and one of the main reasons I dislike
putting functions and constants in namespaces, as this ends up sort of
like OO without inheritance and confuses the issue of static methods as
you pointed out.However, having said that, in my experience, developers either use
functions or OO, very rarely mixing the two on an extensive basis, and
so name collisions become much less likely between static methods and
namespaced functions.Greg
Sorry, I was busy with something else and forget about this thread for a
while.
I've tried ::test::foo() but it calls the namespaced function too.
I'm trying two approaches to solve this issue:
-
raise an error on function call if the ambiguity is found
-
raise an error on function declaration if the ambiguity is found
With (1) the problem I have is: how to provide a way to solve the ambiguity?
ie, how can one say if he's referring to the class method or the namespaced
function?
With (2) one element cannot exist if the other exist. This is a more
sensible approach if you're in the "there cannot be two elements which are
referred with the same name" way of thinking. But considering that php
allows classes and functions to share names... Anyway, the problem with this
approach is that the extra checks needed must be done at runtime...
Bottom line, if anyone has more ideas... I'll be more than grateful to hear
them.
Best Regards,
Martin Alterisio.