-2

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
Dan
  • 451
  • 3
  • 13
  • *"But I am not seeing any output when I load up the page."* - Well, you should with your error reporting set. If that doesn't throw you a parse error, then I have my own views as to what's "not" going on. I'll let you take it up with the answers below. – Funk Forty Niner Jun 14 '17 at 01:57
  • You guys down there paying attention up here? Read the question again; completely. Specifically ***"I get no output"***. Edit: I guess not. – Funk Forty Niner Jun 14 '17 at 02:01
  • *"I get no output that I can see."* - I don't believe you `error_reporting(E_ALL); ini_set("error_reporting", 1);`. – Funk Forty Niner Jun 14 '17 at 02:11
  • @Fred-ii- I wonder how long it will take to find the next syntax error with no output. I tried running it in the console, and whaddaya know? It didn't complain! Then I looked up error reporting, LOL. Take careful aim at your foot... (the OP's foot, that is) – Tim Morton Jun 14 '17 at 02:28
  • @Fred-ii- what are you going on about?? I didn't get an output or an error, and just now I found out the company is having wifi difficulties so I suspect that some of my issue may have been caused by my file not actually uploading to the server. – Dan Jun 14 '17 at 02:31
  • What he's going on about is that you shot yourself in the foot with `ini_set("error_reporting", 1);` You just told PHP to NOT tell you what's wrong. – Tim Morton Jun 14 '17 at 02:32
  • And the answers below, while accurate, didn't address how to help you learn how to catch your own mistake. Frankly, I wouldn't have seen it without his comment, because I don't change the error level in my scripts, so I just glossed over it, although I wondered why you added the second one. But then again, check my rep against theirs... – Tim Morton Jun 14 '17 at 02:38
  • Ok excellent, I have been confused about setting error reporting for a while. What would be the way to tell php to show errors for the specific page I am using? – Dan Jun 14 '17 at 02:41
  • well, since I don't do that in my scripts, what I would try would be `ini_set("error_reporting", E_ALL);` for when I'm developing. Actually, that's not true. I just wouldn't do it. Set it in php.ini according to whether you're in a development or production environment. – Tim Morton Jun 14 '17 at 02:48

2 Answers2

1
var name;

Is not valid syntax, change to:

var $name;
Enstage
  • 2,106
  • 13
  • 20
0

You seem to have simply forgotten the $ in your PHP variable. var name should be var $name:

<?php
  class Game {
    var $name;
    public function show_game() {
      echo $this->name; // Returns "testing game"
    }
  }
  $game = new Game;
  $game->name = "testing game";
  $game->show_game();
?>

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71