0

I have some trouble with my code. I want call object array value in my method, bu have that errors:

Notice: Undefined variable: login_error in C:\xampp\htdocs\frontend\controller\Users.php on line 27

Notice: Trying to get property of non-object in C:\xampp\htdocs\frontend\controller\Users.php on line 27

That is my controller Users.php

class Users extends Controller
{   


    function doLogin(){

        if(isset($_POST['zaloguj'])){
            Users::error($login_error->empty);
        }
    }
}

And Language file where I have my object array.

<?php 
$login_error = (object) array(
    'empty' => 'ERROR TEXT',
    'dberror' => 'ERROR TEXT 2'
);
?>

Global Controller with my error function:

public function error($text){
        echo '<div class="alert alert-danger">
          <strong>Błąd!</strong> '.$text.'</div>';
    }
    public function success($text){
        echo '<div class="alert alert-success">
          <strong>Brawo!</strong> '.$text.'</div>';
    }
    public function info($text){
        echo '<div class="alert alert-info">
          <strong>Uwaga!</strong> '.$text.'</div>';
    }

And loader - I load all my controlers in ONE file.

<?php
require_once('config.php');

///////////////////////////////////
// INCLUDING LANGUAGES
///////////////////////////////////
include('frontend/language/pl_PL.php');

///////////////////////////////////
// INCLUDING CONTROLERS
//////////////////////////////////
require_once('frontend/controller/Controller.class.php');
require_once('frontend/controller/Users.php');

//////////////////////////////////
// INCLUDING MODELS
//////////////////////////////////
require_once('frontend/model/model.php');
require_once('frontend/model/Users.php');

?>
anon
  • 855
  • 12
  • 22
KarambolNaA4
  • 29
  • 1
  • 5
  • 1
    `$login_error` isn't defined in `doLogin` so the variable doesn't exist. What's the question? – Jonnix Nov 30 '16 at 09:53
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Jonnix Nov 30 '16 at 09:55
  • How can I include my language file for all my methods in Controller class? I don't want including my language file in all methods, one by one. – KarambolNaA4 Nov 30 '16 at 10:03

1 Answers1

0

You can write a Language library and get instance of it ( Singleton ) from everywhere like this :

At your frontend/libraries/language.php :

class Language{

    private static $instance;

    public $login_error;

    /*
        ..etc
    */

    public function __construct(){
        self::$instance = $this;
    }

    public function load_file($file){
        include $file;
        $this->login_error = $login_error;
        /*
            And other variables..
        */
    }

    public static function &get_instance(){ // Singleton get instance

        return self::$instance;

    }

At your ONE File where you load every class :

require_once('config.php');

///////////////////////////////////
// INCLUDING LANGUAGES
///////////////////////////////////
include('frontend/libraries/language.php');

$language = new Language();

$language->load_file('frontend/language/pl_PL.php');

///////////////////////////////////
// INCLUDING CONTROLERS
//////////////////////////////////
require_once('frontend/controller/Controller.class.php');
require_once('frontend/controller/Users.php');

//////////////////////////////////
// INCLUDING MODELS
//////////////////////////////////
require_once('frontend/model/model.php');
require_once('frontend/model/Users.php');

And at your Controllers get Singleton instance of Language library like this :

class Users extends Controller{

    private $language;

    public function __construct(){
        parent::__construct();
        $this->language =& Language::get_instance();
    }

    public function doLogin(){

        if(isset($_POST['zaloguj'])){
          Users::error($this->language->login_error->empty);
        }

    }

}
aprogrammer
  • 1,764
  • 1
  • 10
  • 20