OO PHP Accessing public variable from another class
Asked Answered
I

4

18

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 ?

Innuendo answered 10/7, 2013 at 17:12 Comment(0)
C
41

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;
Curricle answered 10/7, 2013 at 17:22 Comment(0)
M
5

you can make it static variable, so you will be able to call it anytime anywhere, the diff is that instead of

$this->lang;

when editing it(Works inside class game only) you do :

self::$lang;

and when you call/edit it (Works everywhere) from anther class you do :

game::$lang

the idea of static class is that its exist only in one instance, so only one $lang exist in your program. but there is no need to load the whole class to get acsess to it.

Marlow answered 10/7, 2013 at 17:25 Comment(0)
W
2

How can I call the result of that variable from another class, lets call it class Check?

A variable doesn't have a result. If you mean to retrieve the state of that variable on a specific object $obj of class game then you can simply do:

$obj->lang

On a side note if $lang is publicly only read only you should protect it by defining it private or protected and create a getter method instead.

If you mean that you want to use the same variable name in another class I'd suggest you to consider inheritance:

class Check extends game { /* now Check has $lang */ }

but the variable of the two objects will be different.

Woadwaxen answered 10/7, 2013 at 17:17 Comment(0)
C
0

Since the property is public, you can access it from outside the class as $objInstance->property. It doesn't matter if you're calling it from a function, procedural script, in another object. As long as you have the instance, you can call it's public property. Ex:

function foo($c) {
    echo $c->lang;
}
foo($check);

Also, some advice on working with objects and such: It's considered better code if you don't create instances of objects in the other objects, but rather pass them in someway (either a setter method or through the constructor). This keeps the classes loosely coupled and results in code that is more reusable and easier to test. So:

class Game
{

...
public function __construct($check, $defaultLang, $get) {

    $this->check = $check;

    $this->lang = $defaultLang;
    if (isset($get['lang']) && !$this->check->isEmpty($get['lang']))
        $this->lang = $get['lang'];
}
...

$game = new Game(new Check(), DEFAULT_LANG, $_GET);
echo $game->check;

The first half of this article is an accessible explanation of what is known as Dependency Injection.

Cutwork answered 10/7, 2013 at 17:16 Comment(1)
As I said I want to call it within another class, not externally in a procedural code or in a simple functionInnuendo

© 2022 - 2024 — McMap. All rights reserved.