O'Reilly logo

PHP 5 Power Programming by Derick Rethans, Stig Sæther Bakken, Andi Gutmans

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.3. Declaring a Class

You might have noticed from the previous example that declaring a class (an object template) is simple. You use the class keyword, give the class a name, and list all the methods and properties an instance of this class should have:

class MyClass {
    ... // List of methods
    ...
    ... // List of properties
    ...
}

You may have noticed that, in front of the declaration of the $name property, we used the private keyword. We explain this keyword in detail later, but it basically means that only methods in this class can access $name. It forces anyone wanting to get/set this property to use the getName() and setName() methods, which represent the class's interface for use by other objects or source code.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required