J
J
JackShcherbakov2018-03-02 00:19:32
PHP
JackShcherbakov, 2018-03-02 00:19:32

Why is there an error when an optional access modifier is missing in a PHP class?

Hello!
Here is a quote from the documentation :


Class methods must be defined with the public, private, or protected modifiers. Methods where there is no modifier definition are defined as public.

Then why not:
class A{
   $attribute = 5; //без модификатора доступа public (который по умолчанию)
}

- the following warning is issued:

Parse error: syntax error, unexpected '$attribute' (T_VARIABLE), expecting function (T_FUNCTION) in C:\OpenServer\domains\comet\php.php on line 4

While the code explicitly specifying the access modifier does its job well:
class A{
   public $attribute = 5; //без модификатора доступа public (который по умолчанию)
}

It seems to me that I missed something somewhere and do not understand. There is also a clear contradiction with the documentation.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2018-03-02
@JackShcherbakov


Attention, the question is: do you declare a method or, after all, a property below?
class A{
   public $attribute = 5;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question