0

I've the followings classes:

  • abstract class utility, which contains general functions (e.g. database connection)
  • class get_conf, which returns the main variables that I will need almost everywhere (e.g. language, login status)
  • class get_data to handle the query to the database and return the results.

This is in my index file:

$init = new get_conf();

$lang = $init->lang;
$status = $init->status;
(...)

$config = new get_data($lang,$status);

This is the construct of class get_data.

class get_data extends utility {

    public function __construct($lang = NULL,$status = NULL) {
        $this->lang = $lang;
        $this->status = $status;
    }

...

Everything work fine, but I don't know how to handle at best during an ajax call. After instantiate the class that I need,

$config = new get_data();

What is the best way to get $lang and $status? At the moment I'm calling again the functions that define their values - get_language(), check_login().

But there is a better way? Should I use sessions? It doesn't sound good to me call every time those function, especially when I have multiple ajax calls in the same page.

EDIT: I'm sorry, is my fault cause I formulated the question in the wrong way. What I would need is to get the variables from Ajax and ok, but I would need to use them in the class

For example, in the class get_data I've this function:

public function get_category($id_cat) {
    $q = "SELECT category FROM p_category WHERE id = '".$id_cat."' AND code = ".$this->lang.";
    return $this->exe_query($q);
}

Cause I'm calling this function both from Ajax and not, depending on the situation, $this->lang is defined only when I call it out of an Ajax request. And even using static var doesn't work with Ajax.

2 Answers 2

1

Write the following member function inside get_data class:

public function getCVar($var){
  $c = new get_data();
  eval('$tmp = $c->'.strtolower($var).';');
  return $tmp;
  }

Then you can get the variables value like below:

echo get_data::getCVar('lang'); echo get_data::getCVar('status');

0

Try it:

class get_data extends utility {
    static $lang = null;
    static $status = null;
    public function __construct($lang = NULL,$status = NULL) {
        if($lang !== null){
            self::$lang = $lang;
        }
        if($status !== null){
            self::$status = $status;
        }
    }
}

$config = new get_data($lang,$status);
echo get_data::$status;
echo get_data::$lang;
2
  • Thank you I think it works great! I'm just getting Notice: Undefined property: get_data::$lang where in the rest of the class I have $this->lang, $this->status, but I guess I should just replace all those with self::lang and self::status, right?
    – supadema
    Sep 5, 2014 at 4:41
  • Yes. Static properties necessary to cause through self :: Sep 5, 2014 at 4:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.