Search code examples
phpclassoopphp-5.6public-method

php public function is not working outside of the class


I have created a simple class, variable, and public function. When I try to initiate an object and call the public function from the class outside of the class, I get no output that I can see.

<?php
error_reporting(E_ALL);
ini_set("error_reporting", 1);

  class Game {
    var name;

    public function show_game() {
      echo $this->name;
    }
  }

  $game = new Game;
  $game->name = "testing game";

  $game->show_game();
?>

From what I understand, the function should echo out testing game. But I am not seeing any output when I load up the page.


Solution

  • var name;
    

    Is not valid syntax, change to:

    var $name;