0

I have a controller class called "query" and another class named "language" to detect the language from the browser and verify it to be one of the available ones. my code looks like this :

in the controller :

Language::detect();

in the "language" class :

public function detect()
{

     $this->_verify(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));


}

private function _verify($input)
{

    $languages=array
    (
        'English'   =>  'en',
        'German'    =>  'de',   
    );

    if (in_array($input,$languages))
    {

        echo $input;

    }

}

the problem is it seems like the method _verify() is called as if it belongs to the controller and I get a "Fatal error: Call to undefined method ....."

how would I go about calling it so it looks for it within the same class?

1
  • You're using scope resolution operator (::) in your controller and you're calling detect() function which uses object reference ($this) of an object that's not instantiated. Why not use $language = new Language;$language->detect()? Commented Apr 5, 2011 at 11:44

5 Answers 5

7

The problem, here, is that you are mixing a static call :

Language::detect();

With non-static methods :

public function detect()


i.e. your detect() method is not static -- but you are calling it as if it were static.

The consequence is that $this, in the detect() method, doesn't quite exist -- as it's a reference to the current object -- which itself doesn't exist, as the method is called statically.

So, PHP is considering that $this points to the class from which the method is called... and the result is that $this points to your controller ; _verify() being private, it cannot be called from that controller ; and this explains the Fatal Error.


You should make up your mind, and either :

  • Use static methods if you want to call them statically
  • Or not call non-static methods as if they were static.
Sign up to request clarification or add additional context in comments.

Comments

2

Yout calling the method without initializing the object first, in this case there is no special variable $this as $this refers to the local object, but there is no object until you do: new Language();

What you should be doing is using the :: operator to specificy that your accessing the method of a class, and not an object:

public static function detect()
{
     self::_verify(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));
}

private static function _verify($input)
{
    $languages=array
    (
        'English'   =>  'en',
        'German'    =>  'de',   
    );

    if (in_array($input,$languages))
    {
        echo $input;
    }
}

notice the following lines have changed:

  • private static function _verify($input)
  • public static function detect()
  • self::_verify(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));

the first to specify that the methods are static and should be accessed statically, and the last line is saying i want to access this method statically as well as locally.

1 Comment

hmmm, @Pascal MARTIN should have had the acceptance as he was first to answer correctly.
1

If you're using "Language::detect" you can not use "$this" in your class but must use "self::"

Comments

1

Your detect method is not static, but you call it as static. So you can't use $this inside this method.

Comments

0

You need an object Language or a static identifier so that you may call a function of an object without actually having it instanced.

If you want the non-static approach you should do something like:

Language $test = new Language();

$test->detect();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.