Just trying to learn PHP. Following the example in the book I study OOP. It describes the creation of a class and an object of this class. Here is the code:

<? php $object = new User; print_r($object); class User { public $name, $password; function save_user() { echo "Текст функции Сэйв_юсер"; } } ?> 

But when trying to start this code in OpenServer, it gives an error:

Parse error: syntax error, unexpected '$ object' (T_VARIABLE) in D: \ WebServer \ domains \ php.learn \ index.php on line 3

But I wrote off everything from the textbook, why doesn't it work?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

PHP 7 works. Try

 public $name; public $password; 

PS

 <? php 

Replaced by

 <?php 

those. remove space.

  • one
    Yes, thanks, for sure, the space does not work!))) Without a space, everything worked !!! - RumusBin
  • Tick ​​with a solution in this case :) - ilyaplot
  1. What would work next print_r($object); need to add __tostring method.
  2. print_r ($ object-> save_user ());
  3. For learning OOP in PHP, I recommend the following book by Zandstra M. - PHP. Objects, patterns and programming techniques, 4th edition.
  • Better just var_dump ($ object); - ilyaplot