John Davidson

Why can't an expression be assigned to a PHP member variable by default?

0 comments
Message:


The following code produces a fatal error in PHP:


class A {}
class B {
private A $a = new A();
}

PHP Fatal error: Constant expression contains invalid operations


Of course, you can move this assignment into B's constructor, and then it will run. But why does PHP limit you to constant values in this context, when the analogous operation is allowed in e.g. Java?



My Answer:
you not sleep at night?

Rate this post

5 of 5 based on 7555 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us