I often see code a function defined without visibility keywords. e.g:
class Foo() {
function bar() {
// ...
}
}
Is it a shorthand of public
function? Is it a good practice to omit it?
class Foo() {
public function bar() {
//..
}
}
As written in the PHP Doc,
Methods declared without any explicit visibility keyword are defined as public.
So, yes, in
class Foo() { public function bar() { //.. } }
Foo::bar()
is public, but omitting the visibility keyword is never a good practice. If it's a fast and ugly script why not, but in other cases you should specify it.
Yes, you are right; when you omit the visibility modifier it means it's public
.
It's a holdover from PHP 4 which did not support visibility operators. This feature is included for backward compatibility.
You can read more about it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With