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.
<?php
class Foo {
public static $name;
public static function GetName() {
return self::$name;
}
}
class Bar extends Foo {
public static $name = 'bar';
}
echo Bar::GetName();
?>
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.
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)...
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?
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.
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.