Page 1 of 1

time to demonstrate your skills... a brainteaser

Posted: Fri Oct 27, 2006 12:46 pm
by timvw
What is the output of the following program?

Code: Select all

<?php
class Foo {
        public static $name;

        public static function GetName() {
                return self::$name;
        }
}

class Bar extends Foo {
        public static $name = 'bar';
}

echo Bar::GetName();

?>

Posted: Fri Oct 27, 2006 1:17 pm
by feyd
If memory serves, nothing.

Posted: Fri Oct 27, 2006 1:18 pm
by Zoxive
Beginner answer = 'bar' ?

-NSF

Posted: Fri Oct 27, 2006 1:25 pm
by John Cartwright
I thought it was nothing as well, the child class inheres from the parent, not the other way around.

Posted: Fri Oct 27, 2006 1:49 pm
by Burrito
I'd guess nothing...

/me tries it

Posted: Fri Oct 27, 2006 2:06 pm
by Luke
I'd say nothing as well because $name is a static property of Foo, not Bar

Posted: Fri Oct 27, 2006 2:17 pm
by RobertGonzalez
I'd say nothing, because the child cannot instantiate the parent. There is no constructor, not even an assumed one. Though I could be way off, it just seems like the $name var in the child class has no way of telling the parent what its own value is.

Posted: Fri Oct 27, 2006 2:22 pm
by feyd
Terse explanation/example here: viewtopic.php?p=302378#302378

Posted: Fri Oct 27, 2006 2:29 pm
by timvw
The answer is in the manual: http://be.php.net/manual/en/language.oop5.static.php..

I guess i should have posted the c# version ;)

Code: Select all

    public class Foo
    {
        static Foo()
        {
            Console.WriteLine(".ctor Foo");
        }

        public static string Name;
    }

    public class Bar : Foo
    {
        static Bar()
        {
            Console.WriteLine(".ctor Bar");
            Name = "hihi";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Bar.Name ?? "oops");

            Console.Write("{0}Press any key to continue...", Environment.NewLine);
            Console.ReadKey();
        }
    }
The point is that Bar::GetName is actually identified as Foo::GetName.. So when you make the call, only Foo needs to be available... And thus the c# version would output: .ctor foo, and then oops (since Instance is still null because Bar's static constructor hasn't been called)...

Posted: Sat Oct 28, 2006 4:11 am
by Ollie Saunders
Yeah its nothing.
I always thought that was considered a bug, or an incomplete implementation at least. It has bitten me in the arse once or twice. But this comment:
Everah wrote:I'd say nothing, because the child cannot instantiate the parent. There is no constructor, not even an assumed one. Though I could be way off, it just seems like the $name var in the child class has no way of telling the parent what its own value is.
makes me doubt that. How would this in any way be desirable?

Posted: Sat Oct 28, 2006 4:48 am
by Chris Corbyn
It sort of seems expect to me, Java's exactly the same:

Code: Select all

class StaticInheritanceTest
{
        public static void main(String[] args)
        {
                System.out.println(Bar.getName());
        }
}

class Foo
{
        public static String name = "I am Foo";

        public static String getName()
        {
                return name;
        }
}

class Bar extends Foo
{
        public static String name = "I am Bar";
}
Prints "I am Foo".

PHP6 will introduce a new way to use the "static" keyword however so rather than "self::name" which literally gets the name variable from itself you can use "static::name" which will give the behaviour you might have expected.

Posted: Sat Oct 28, 2006 11:44 am
by Ollie Saunders
PHP6 will introduce a new way to use the "static" keyword however so rather than "self::name" which literally gets the name variable from itself you can use "static::name" which will give the behaviour you might have expected.
Oh wow. You do have your ears close to the ground.