Why does my code not work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

27th Aug 2020, 4:02 PM
Michał Cukrowski
Michał Cukrowski - avatar
6 Answers
+ 1
Michał Cukrowski within the constructor u can just add global keyword before the variable name: global $array; $array[] = $ha;
27th Aug 2020, 4:21 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 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.
27th Aug 2020, 4:10 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
27th Aug 2020, 4:11 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 1
Thanks a lot!! I knew I was missing something very simple 😂
27th Aug 2020, 4:23 PM
Michał Cukrowski
Michał Cukrowski - avatar
+ 1
Hope that was helpful 😅
27th Aug 2020, 4:24 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
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?
27th Aug 2020, 4:14 PM
Michał Cukrowski
Michał Cukrowski - avatar