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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to