The Street Way

Public Accesss Control Modifiers

Public

Public properties and methods are accessible from anywhere in your script, which makes this modifier the easiest to use. Using this terminology is deprecated and may generate compiler warnings. Take a look at the following code:

Ex.   class A {
            public $Name;

            public function say( ) {
                    print "Hello!\n";
            }
    }

    class B extends A {
            public function say( ) {
                    print "Yap!\n";
            }
    }

    $p = new B;
    $p->Name = "p";
    print $p->Name;

That code works in precisely the same way as before the public keyword has not made any difference. This is because, by default, all class methods are public.

While the public keyword is not needed, I recommend you use it anyway it is a good way to remind people who read your code that a given method is indeed public. It is also possible that class methods without an access modifier may be deprecated in the distant future.

You always need to specify an access modifier for properties and be more specific with public or one of the other keywords.

0 comments:

Post a Comment