Reputation: 4847
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?
Upvotes: 0
Views: 383
Reputation: 57278
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.
Upvotes: 2
Reputation: 2001
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();
Upvotes: 0
Reputation: 2343
Your detect
method is not static, but you call it as static. So you can't use $this
inside this method.
Upvotes: 1
Reputation: 4583
If you're using "Language::detect" you can not use "$this" in your class but must use "self::"
Upvotes: 1
Reputation: 401182
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 :
static
methods if you want to call them staticallystatic
.Upvotes: 7