0
Why does my code not work?
I want it to create an object and automatically put its name into an array. https://code.sololearn.com/wiwpL2ASz44Z/?ref=app
6 Respuestas
+ 1
Michał Cukrowski within the constructor u can just add global keyword before the variable name:
global $array;
$array[] = $ha;
+ 1
You are accessing the class property$array within the constructor, in php it is mandatory to access the class properties using $this reference..
And outside the class u r accessing $array directly without the the object
var keyword is deprecated from PHP v5.0
and public can be used alternatively or when not defined it is public by default
the property $a defined public within the class, please understand that it is not same as the variable outside the class, which u have used to create object.
+ 1
Thanks a lot!! I knew I was missing something very simple 😂
+ 1
Hope that was helpful 😅
0
Sorry, I made a little typo here, I wanted $array to be outside the class as a global variable but being desperate I put it inside and mistakenly saved :)) I've already changed it, what should I do in such a situation?