OO PHP Accessing public variable from another class
oop, php
Solution
if you mark the `public $lang;` as static:
public static $lang;
you can access it via `game::$lang;`
if not static, you need to make an instance of game and directly access it:
$game = new game;
$game->lang;
static call inside of (current) class:
self::$lang;
late static bound call (to inherited static variable):
static::$lang;
call from child class to parent:
parent::$lang;
normal call inside of an instance (instance is when you use `new Obj();`):
$this->lang;
BTW: variables defined by `define('DEFAULT_LANG', 'en_EN');` are GLOBAL scope, mean, can access everywhere!
<?php
define('TEST', 'xxx');
class game {
public function __construct() {
echo TEST;
}
}
//prints 'xxx'
new game;
Problem
I have a class like the following: ``` class game { public $db; public $check; public $lang; public function __construct() { $this->check = new check(); $this->lang = DEFAULT_LANG; if (isset($_GET['lang']) && !$this->check->isEmpty($_GET['lang'])) $this->lang = $_GET['lang']; } } ``` As you can see I have a public variable `$lang` that is also defined via the contructor. The proble is that I want to access the result of this variable from other classes that are not directly related to this class, since I don't want to redeclare it for each different class. So for example how can I call the result of that variable from another class, lets call it `class Check` ?