Answer the question
In order to leave comments, you need to log in
Why are undefined object properties displayed in PHP?
Good morning. I deliberately made a mistake in the name of the variable when it was defined. But the code still works. What can you advise?
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
class Customer {
var $name1;//error here
var $lastname;
function getName() {
echo 'Name: ';
echo $this -> lastname;
}
function setName($name) {
$this -> name = $name;
$this -> getName();
}
}
$my_client = new Customer;
$my_client -> lastname = 'Doe';
$my_client -> setName('John');
echo '<br>';
echo $my_client -> name . ' (test)<br>';
echo $my_client -> lastname;
?>
Name: Doe
John (test)
Doe
Answer the question
In order to leave comments, you need to log in
Instead of echo, it is better to do var_dump or at least print_r on the entire object, they will clarify a lot for you.
In this case, when you do $this -> name = $name;
, a new name property is created on the object.
object(Customer)[1]
public 'name1' => null
public 'lastname' => string 'Doe' (length=3)
public 'name' => string 'John' (length=4)
Read about "magic methods" . Special attention to __get() and __set().
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question