PHP OOP Quick Review:

  • All properties must be declared and be preceded by one of the visibility keywords: public, protected, or private. (As with every rule, there is one exception when the visibility keyword is optional.)
  • Using a visibility keyword is optional when defining a method, but is recommended. If no keyword is used, the method is automatically treated as public.
  • “Method” is simply OOP (Object-Oriented Programming) terminology for a function inside a class. Use the function keyword when defining a method.
  • Use the new keyword to create an instance of a class (an object).
  • The $this variable inside a class refers to the current object.
  • Use the -> operator to access an object’s methods or properties (there is no space between – & >).
  • When accessing a property with the -> operator, omit the $ sign.
  • Attempting to access a protected or private property or method from an external script triggers a fatal error.
  • Use getter and setter methods to access protected and private properties and methods from an external script.

Source: “PHP Object-Oriented Solutions” by David Powers. A definite reference material to keep side by side with your famous “Gang of Four” book (What? You don’t own it?)

There’s only one thing that bothers me a bit. It says PHP triggers a fatal error when trying to access any private or protected properties from an external script. Does only trying to access them from an external script trigger error? Isn’t it that anything outside the class (in the same script) trying to access protected or private property triggers a fatal error?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: