0

I have a php class like the following and got error saying invalid operation. I tried Sell::$a, self::$a and $this->a, Foo::$a, none of them worked. So I'm wondering if it's possible with PHP. I'm using php7.1. With Perl, there'd be no problems.

<?php

class Foo {
    public static $a = 1;

    public static $b = $a; /* want to assign $a to $b here */

    ...
}

1 Answer 1

1

No, you can't, since PHP supports static initialization with literals and const expression.

You can't even use __get since

Member overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods can not be declared static.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I'd spent a hour or so trying to get it to work.
@Shiping oh you're welcome, i love PHP but this is one of those cases where i can't understand why they are not implementing it (there are also several proposal for this)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.