-2

I created one class and created some member variables ,

i am not getting why this error is coming ,when ever i am writing private or public access specifiers before the memeber variables , then error is disappering ,

what is the reason behid it.

is it not taking public as default ??

code

<?php
class abc
{
    $v = "g";

 $array = ['name'=>'test','age'=>5];

}

?>
tereško
  • 56,151
  • 24
  • 92
  • 147
sradha
  • 2,126
  • 22
  • 43
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Nigel Ren May 27 '18 at 06:28
  • 1
    You claim to know php. And cakephp framework. Did you lie in your profile description? – tereško May 27 '18 at 09:47

2 Answers2

2

In PHP class member declarations require a visibility keyword (public, protected, or private) or the deprecated var keyword. When using var, the visibility will be public.

You may have gotten mixed up with class method declarations, where the visibility keyword can be omitted, defaulting to public visibility.

faintsignal
  • 1,746
  • 3
  • 19
  • 28
  • so by default is private , is it ? – sradha May 27 '18 at 05:53
  • @sradha The default visibility is never private in PHP. For class members (properties) there *must* be one of the above-mentioned keywords used to declare the member variable. – faintsignal May 27 '18 at 11:35
0

Just correct by this

<?php
class abc
{
    protected $v = "g";

    protected $array = ['name'=>'test','age'=>5];

}

The reason ? it's just what @faintsignal says.

Goms
  • 1,921
  • 3
  • 13
  • 32