tags:

views:

56

answers:

3

Hello there i have 2 classes

  1. for DB
  2. for language

i want to use my language things in the DB so it outputs the result

ex :

class db_control{

        var $db_connection, $lang_var;
        //create the function for the connection
        function db_connect(){
            //define some variables
            global $db_host, $db_username, $db_password, $db_name, $lang_var;
            $this->db_connection = mysql_connect("$db_host","$db_username","$db_password")or die("can't connect to server with these informations");
            //checl that the connection is established
            if($this->db_connection){
                echo $lang_vars->getvar("$langvals[lang_con_est]");
            }

but this

$lang_vars->getvar("$langvals[lang_con_est]");

doesn't work i mean it outputs many problems and am sure my problem is that i didn't define my variables and classes correctly

P.S : the language class is in file called language.php and this part is in DB.MySQL.php

EDIT : this is the language class

class lang_vars{
    public static function getvar($variable){
        return $variable;
    }
}

i want the DB class to display text from the language class thats why i used

 echo $lang_vars->getvar("$langvals[lang_con_est]");

but it doesn't work cuz when i declare the language class $lang_vars = new lang_vars; inside the db_control it shows error unexpected T_something expected T_Function and when i declare it outside nothing up hope i made things more clear now

+1  A: 

Make your language class methods static . Read more here.

class LangClass
{
    public static function getvar()
    {
        // your code here
    }
}

Then, you can use its functions without creating objects like this:

  $LangClass::getvar("$langvals[lang_con_est]");
shamittomar
can you be more specific ?which class i make it static and which not
SAFAD
@SAFAD, I have updated the answer. You need to make functions of your language class static by putting the `static` keyword in its definition.
shamittomar
well i did but this weird error apearedunexpected T_PAAMAYIM_NEKUDOTAYIM on the line where i called $LangClass::getvar("$langvals[lang_con_est]");
SAFAD
@SAFAD: check http://www.google.com/search?q=paamayim+nekudotayim for more info on that. It's 'double colon' in hebrew.
Adriano Varoli Piazza
+1  A: 

This can do the trick.

$lang_vars = new LanguageClassOrWhateverItIsCalled();
$lang_vars->getvar($langvals[lang_con_est]);

But maybe you should think of making it a static method. In that case you can call it with:

LanguageClassOrWhateverItIsCalled::getVar($langvals[lang_con_est]);

You can define the method static like:

public static function getVar() {
    // Do something
}

Edit: @SAFAD

You should use the static method for this. To make this work, be sure your class language.php is loaded. To do so just add in the DB.MYSQL.php file the following line:

require_once('language.php');

class db_control {
    ...

Make sure you have the right path to the language.php file.

Then you should call the method in db_control class like this:

if($this->db_connection){
    echo lang_vars::getvar("$langvals[lang_con_est]");
}

Besides, what is the use of a function like this? You should either do:

if($this->db_connection){
    echo $langvals[lang_con_est];
}

or change your static getvar method to:

public static function getvar($variable){
    return $langvals[$variable];
}

and your function call to:

if($this->db_connection){
    echo lang_vars::getvar("lang_con_est");
}
Stegeman
thanks alot mani had to create the object inside each function in the classis there any solution to create it once and use it everywhere ?
SAFAD
am sorry for my previous commenti found out that i can get only1 language variable using that linewhich meansif i have another query like INSERT or something i can't use that line to show if the query is inserted or not
SAFAD
my wrongthat function doesn't work at all
SAFAD
+2  A: 

Any reason why you are still using PHP4 syntax?

When creating an instance of the db_control class, pass the object to be stored as $lan_var into the constructor or set it via a dedicated setter. See Dependency Injection.

class DBControl
{
    protected $_lang;
    public function __construct($lang = NULL)
    {
        if($lang !== NULL) {
            $this->_lang = $_lang;
        }
    }
    public function setLang($lang)
    {
        $this->_lang = $lang;
    }
}

Then do either

$dbControl = new DBControl(new LangThing);

or

$dbControl = new DBControl;
$dbControl->setLang(new LangThing);

Also, get rid of the globals. Pass those in via Dependency Injection too.

Gordon
what you mean php4 syntax ?i never used PHP4
SAFAD
@SAFAD you are using the `var` keyword to declare class properties. [That's PHP4 syntax](http://php.net/manual/en/oop4.php). Also, you are not using any [Visibility declarations](http://de.php.net/manual/en/language.oop5.visibility.php). Check out [the manual](http://de.php.net/manual/en/language.oop5.php) and [this tutorial](http://thinkvitamin.com/dev/getting-started-with-oop-php5/) and this [introduction](http://stackoverflow.com/questions/2206387/learning-php-class/2206835#2206835)
Gordon
ok okmaybe i am still using a bit old thingswhat shall i replaec "var" with ?
SAFAD
@SAFAD with either `private`, `protected` or `public`. See the links I've added to my previous comment and see the code I've provided.
Gordon