As of version 5.4, STRICT error is issued for the presented case.
If you are not seeing error messages on the screen may be due to the error report settings.
If you’re using PHP version 5.4 or higher, set the bug report to allow reporting STRICT errors
One suggestion, in a development environment, it is recommended to display every kind of error and then fix every kind of problem that appears, even those of type STRICT and DEPRECATED.
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
The decision on how to set up the bug report depends on each case. There are cases where it is necessary to hide errors of type STRICT and DEPRECATED. An example, it is legacy systems where it becomes unviable to fix and convert many codes. (lack of time, lack of investments, lack of permission from a superior, etc).
Returning to the subject, it is recommended that you correct within the standard, explicitly stating the visibility and type of methods, such as the properties of a class.
Example
HOW NOT TO:
class Foo {
function Bar() {
}
}
Correct by setting the visibility. In this example we will use public
.
class Foo {
public function Bar() {
}
}
The method is not static, so it must be invoked by an instance
$c = new Foo;
$c->Bar();
If you try Foo::Bar();
will present STRICT errors and other subsequent errors such as access to $this
out of context, for example. And this causes a snowball of errors.
Dribbling the bug report
As mentioned above, there may be situations where it is impracticable to correct all errors. For these cases, you can make the following configuration
/*
Aqui diz que deve reportar todos os erros exceto os do tipo STRICT e DEPRECATED.
*/
ini_set('error_reporting', E_ALL & ~E_STRICT & ~E_DEPRECATED);
/*
true: para ambiente de testes (o seu localhost)
false: para ambiente de produção (o site online, no servidor)
*/
ini_set('display_errors', false);
/*
Opcionalmente pode ligar o log de erros e então definir uma local onde deseja salvar esses avisos.
*/
ini_set('log_errors', true);
/*
Local onde serão gerados os arquivos de logs de erro.
Apenas cuidado pois dependendo da quantidade de acessos e de erros, esses arquivos podem ficar enormes. O ideal é estarem sempre vazios. Portanto, qualquer erro pequeno, sempre corrija o mais rápido possível
*/
ini_set('error_log', BASE_DIR.'..'.DS.'logs'.DS.'php'.DS.'PHP_errors-'.date('Ym').'.log');
Gambiarra, invoking non-static method in static method
class Foo {
public function Bar() {
}
public static function Bar2() {
$c = new Bar;
return $c->Bar();
}
}
Foo::Bar2();
The problem here is it doesn’t make sense. You are just creating a faxed one to make code more "elegant" and just consuming memory and unnecessary processing.
A solution is to create definitions consistently with the ultimate goal of what needs to be used in this class.
Avoid gambiarras and hide errors of type STRICT and DEPRECATED. Usually, errors at this level, in the short or medium term become FATAL.
What’s in this class
Crud
? It has her code?– Wallace Maxters
Protecteds methods only
– Thalles Daniel