0

I have a class and I am including the players.php file inside it.

class My_Class {    
    private $player_types;

    public function __construct() {
        $this->player_types = 'classic';
        require_once('players.php');
    }

    public function getPlayerTypes() {
        return $this->player_types;
    }
}

$mc = new My_Class();

How can I call getPlayerTypes function from players.php?

Also if its better to maybe use static method?

3
  • 1
    what type of code written in players.php? Commented Nov 21, 2013 at 11:36
  • I am asking that is php code is general script or it is based on OOP concept? Commented Nov 22, 2013 at 6:39
  • No, its not oop in players.php. Commented Nov 22, 2013 at 11:16

3 Answers 3

1

Just write :

$result = $this->getPlayerTypes();
echo $result;

inside the players.php. Definitely this will work.

Sign up to request clarification or add additional context in comments.

Comments

1

Since the getPlayerTypes() is a method defined in the class My_Class, if you want to call that method from players.php you should instantiate a new My_Class object in that file and call the getPlayerTypes() there.

//player.php
$mc = new My_Class();
$playerTypes = $mc->getPlayerTypes();
echo $playerTypes

and remove that

require_once('players.php');

from your class :)

1 Comment

My class needs to be instantiated in main.php file, so I cannot do this in players.php Also, when I try this $playerTypes = $mc->getPlayerTypes(); in player.php I get Undefined variable: mc
0

Since you are instantiating the class with the variable $mc, you'd call the function using

$mc->getPlayerTypes();

Or you can assign a variable to the result,

$result = $mc->getPlayerTypes();
echo $result;

1 Comment

When I try this $mc->getPlayerTypes(); in player.php I get Undefined variable: mc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.