Not being able to acces an atribute that exists PHP - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, October 6, 2022

Not being able to acces an atribute that exists PHP

I'm trying to access the attribute expireDate that exists in my classes Food and Drink but when I try to access it through a common method of the classes, it gives the error Warning: Undefined property: Drink::$expireDate. Even if before and after the invocation of said method I print the __toString of the instances and the attribute I'm talking about is there.

for ($i = 0; $i < count($this -> slots); $i++) {
                for ($j = 0; $j < count($this -> slots[$i]); $j++) {
                    if ($this->slots[$i][$j] != null) {
                        if (get_class($this->slots[$i][$j]) == "Food" || get_class($this->slots[$i][$j]) == "Drink") {
                            echo $this -> slots[$i][$j]-> expireDate;  <-- Giving the error on this line
                        }
                     }
                 }
}

When using the method the output is:

Weight: 7 Price: 20 Is new: true Name: RedBull Expire Date: 06/10/2012 Tax: 30 Is alcoholic: 1Volume: 200
Weight: 1 Price: 25 Is new: true Name: Absolut Vodka Expire Date: 06/10/300 Tax: 21 Is alcoholic: 1Volume: 1000
Weight: 2 Price: 2 Is new: true Name: Coca Cola Expire Date: 06/10/2020 Tax: 5 Is alcoholic: Volume: 2000
Weight: 4 Price: 10 Is new: true Name: Arroz Integral Expire Date: 06/10/2026 Tax: 10 Type: Cereal,Seco
Weight: 6 Price: 4 Is new: true Name: Tomate Frito Expire Date: 12/06/2020 Tax: 10 Type: Salsa,Verduras
Weight: 4 Price: 2.5 Is new: true Name: Iogurt Expire Date: 07/10/2022 Tax: 10 Type: Lacteo,Postre
Weight: 4 Price: 18 Is new: true Name: pañuelo Warranty Due Date: 04/10/2024Purchase Date: 10/04/2022Tax: 21
Weight: 400 Price: 10200 Is new: true Name: coche Warranty Due Date: Purchase Date: 10/04/2022Tax: 21
Weight: 150 Price: 4300 Is new: true Name: moto Warranty Due Date: Purchase Date: 10/04/2022Tax: 21


Warning: Undefined property: Drink::$expireDate in C:\xampp\htdocs\M07-php\UF1\Practica\Models\Warehouse.php on line 141

Warning: Undefined property: Drink::$expireDate in C:\xampp\htdocs\M07-php\UF1\Practica\Models\Warehouse.php on line 141

Warning: Undefined property: Drink::$expireDate in C:\xampp\htdocs\M07-php\UF1\Practica\Models\Warehouse.php on line 141

Warning: Undefined property: Food::$expireDate in C:\xampp\htdocs\M07-php\UF1\Practica\Models\Warehouse.php on line 141

Warning: Undefined property: Food::$expireDate in C:\xampp\htdocs\M07-php\UF1\Practica\Models\Warehouse.php on line 141

Warning: Undefined property: Food::$expireDate in C:\xampp\htdocs\M07-php\UF1\Practica\Models\Warehouse.php on line 141

Weight: 7 Price: 20 Is new: true Name: RedBull Expire Date: 06/10/2012 Tax: 30 Is alcoholic: 1Volume: 200
Weight: 1 Price: 25 Is new: true Name: Absolut Vodka Expire Date: 06/10/300 Tax: 21 Is alcoholic: 1Volume: 1000
Weight: 2 Price: 2 Is new: true Name: Coca Cola Expire Date: 06/10/2020 Tax: 5 Is alcoholic: Volume: 2000
Weight: 4 Price: 10 Is new: true Name: Arroz Integral Expire Date: 06/10/2026 Tax: 10 Type: Cereal,Seco
Weight: 6 Price: 4 Is new: true Name: Tomate Frito Expire Date: 12/06/2020 Tax: 10 Type: Salsa,Verduras
Weight: 4 Price: 2.5 Is new: true Name: Iogurt Expire Date: 07/10/2022 Tax: 10 Type: Lacteo,Postre
Weight: 4 Price: 18 Is new: true Name: pañuelo Warranty Due Date: 04/10/2024Purchase Date: 10/04/2022Tax: 21
Weight: 400 Price: 10200 Is new: true Name: coche Warranty Due Date: Purchase Date: 10/04/2022Tax: 21
Weight: 150 Price: 4300 Is new: true Name: moto Warranty Due Date: Purchase Date: 10/04/2022Tax: 21


source https://stackoverflow.com/questions/73965310/not-being-able-to-acces-an-atribute-that-exists-php

No comments:

Post a Comment