-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.

9
  • "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. Commented Jun 14, 2017 at 1:57
  • You guys down there paying attention up here? Read the question again; completely. Specifically "I get no output". Edit: I guess not. Commented Jun 14, 2017 at 2:01
  • "I get no output that I can see." - I don't believe you error_reporting(E_ALL); ini_set("error_reporting", 1);. Commented Jun 14, 2017 at 2: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) Commented Jun 14, 2017 at 2: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. Commented Jun 14, 2017 at 2:31

2 Answers 2

1
var name;

Is not valid syntax, change to:

var $name;
Sign up to request clarification or add additional context in comments.

Comments

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! :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.