Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Getting Started with Objects with PHP V5

By Matt Zandstra
2005-07-01


Keywords: Can we have a little privacy in here?

You have already seen the public keyword in relation to property declarations. This keyword denotes a property's visibility. In fact, the visibility of a property can be set to public, private, and protected. Properties that are public can be written to and read from outside the class. Properties that are private can only be seen within the object or class context. Properties that are protected can only be seen within the context of the current class or its children. (You will see this in action in the Inheritance section.) You can use private properties to really lock down our classes. If you declare your properties private and attempt to access them from outside the class' scope (as shown in Listing 7), the PHP engine will throw a fatal error.Listing 7. Attempting to access your properties from outside the class' scope



class Dictionary {
private $translations = array();
private $dictio;
private $type;

function __construct( $type, DictionaryIO $dictio ) {
$this->type = $type;
$this->dictio = $dictio;
}

// ...
}

$en = new Dictionary( "En", new DictionaryIO() );
$en->dictio = null;

This outputs the following:



Fatal error: Cannot access private property
Dictionary::$dictio in...

As a rule of thumb, you should make most properties private, then provide methods for getting and setting them if necessary. In this way, you can control a class' interface, making some data read-only, cleaning up or filtering arguments before assigning them to properties, and providing a clear set of rules for interacting with objects.

You can modify the visibility of methods in the same way as properties, adding public, private, or protected to the method declaration. If a class needs to use some housekeeping methods that the outside world need not know about, for example, you can declare them private. In Listing 8, a get() method provides the interface for users of the Dictionary class to extract a translation. The class also needs to keep track of all queries and provides a private method, logQuery(), for this purpose.

Listing 8. A get() method provides the interface for users of the Dictionary class



function get( $term ) {
$value = $this->translations[$term];
$this->logQuery( $term, $value, "get" );
return $value;
}

private function logQuery( $term, $value, $kind ) {
// write log information
}

Declaring logQuery() as private simplifies the public interface and protects the class from having logQuery() called inappropriately. As with properties, any attempt to call a private method from outside the containing class causes a fatal error.



Tutorial Pages:
» Why you need to know objects and classes, and how to use them
» What are classes and objects?
» A first class
» Properties
» Methods
» The constructor
» Keywords: Can we have a little privacy in here?
» Working in class context
» Inheritance
» Summary
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.