0

Foo.php

abstract class Foo{
    protected static $Bar;
    public __construct(){
        Foo::$Bar = "foobar";
    }
}

Foo2.php

//require_once Foo.php
class Foo2 extends Foo{
    public static function Func(){
        echo Foo::$Bar;
    }
}

End point

//require_once Foo2.php
Foo2::Func();

Then the page shows nothing. It should write "foobar". What am I doing wrong?

If I write

protected static $Bar = "foobar";

The code works for the 80% of cases. But there is a problem:

protected static $Bar = new AnotherClass();

Parse error: syntax error, unexpected 'new' (T_NEW)

Quarktum
  • 669
  • 1
  • 8
  • 26
  • 1
    `protected static $Bar = 'foobar';` – fiction Aug 10 '14 at 19:06
  • It almost worked in a 100%. Ty. But a new problem appeared. I will edit the question, so that you can see the new problem. – Quarktum Aug 10 '14 at 19:10
  • The issue is that there is no 'automatic' routine that is run for classes to initialize 'static' data. When creating an instance there is the 'constructor' that is run automatically. Not so, for anything that is 'static'. However, you can 'roll your own' techniques and make them standard for your application. This may help: **[how-to-initialize-static-variables](http://stackoverflow.com/questions/693691/how-to-initialize-static-variables)**. There are various other techniques. – Ryan Vincent Aug 16 '14 at 18:36
  • @RyanVincent that is the answer, it would be better to put it as an answer instead of a comment. Can you do that please? – dxvargas Jun 17 '16 at 21:36
  • imo, It is a duplicate of: http://stackoverflow.com/questions/693691/how-to-initialize-static-variables. Thanks for the notification. If you have any questions I will gladly clarify. – Ryan Vincent Jun 17 '16 at 21:54

0 Answers0