P
P
pola_pumpkin2020-10-14 12:38:02
PHP
pola_pumpkin, 2020-10-14 12:38:02

Is declaring a property as an object a mistake?

Hello, I can't figure out why I can't access the array.
I decided to declare the property as an object and work through it

public $shop;
 
        public function __construct(){
            $this->shop = new Shop;
        }
 
        #1.Showing shop basket for user
        public function showBasket(){
            if(!empty($this->array)){
                foreach($this->array as $key => $value){
                    $this->shop->render($key);
                    echo "<br><br>";
                }
            }
        }


At the output I get the following picture:
5f86c6d785c5f916347144.png

https://pastebin.com/PfN2zrdB - link to the code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-10-14
@FanatPHP

That is, do you sincerely believe that if you write
$this->shop = new Shop;
this variable in some place in the code, it will immediately have access to all objects of the Shop class ever created in the code?
And your example can be simplified to this

class Person {
        public function __construct(){
            $this->name = '';
        }
 
        public function showname(){
            echo $this->name;
        }
}
$name = "Вася";
$person = new Person;
$person->showname();

and then wonder why it doesn't output anything?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question