time to demonstrate your skills... a brainteaser

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

time to demonstrate your skills... a brainteaser

Post 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();

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If memory serves, nothing.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Beginner answer = 'bar' ?

-NSF
Last edited by Zoxive on Fri Oct 27, 2006 1:26 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I thought it was nothing as well, the child class inheres from the parent, not the other way around.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I'd guess nothing...

/me tries it
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I'd say nothing as well because $name is a static property of Foo, not Bar
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Terse explanation/example here: viewtopic.php?p=302378#302378
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Post Reply