Home arrow PHP arrow Page 2 - Enforcing Object Types in PHP: Using the Type Hinting Feature in PHP 5

The “Type Hinting” feature of PHP 5: taking an in-depth look - PHP

Here we are again. Welcome to the last tutorial of the series “Enforcing object types in PHP.” As this article’s title suggests, this series introduces the basics of object type enforcement in PHP, covering some of the most common methods for checking types of objects in both PHP 4 and PHP 5 respectively.

TABLE OF CONTENTS:
  1. Enforcing Object Types in PHP: Using the Type Hinting Feature in PHP 5
  2. The “Type Hinting” feature of PHP 5: taking an in-depth look
  3. A practical example: using “Type Hinting” within a web page generator class
  4. Putting “Type Hinting” to work: building object-based web documents
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 01, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you’ve been working with PHP 5 for a while, then you’ll probably know what “Type Hinting” is about. If you don’t, let me point you in the right direction. In short, the “Type Hinting” feature will allow you to force parameters of functions (or methods) to be objects of a specific type. As you’ll see, this is very useful for filtering input objects that are passed to a class method, without having to worry whether these objects belong to the correct type or not. If the condition isn’t met, a fatal error is triggered.

The basic syntax of “Type Hinting” can be seen in the following example:

// class 'MessageLister'
class MessageLister {
    public function __construct(Message $message) {
        echo $message->getMessage();
    }
}
// class 'Message'
class Message {
    public $message;
    public function __construct($message) {
        $this->message=$message;
    }
    public function getMessage(){
        return $this->message;
    }
}

As you can see, in the above example I defined two simple classes. The first one, “MessageLister,” takes care of displaying basic string messages, while the second one, “Message,” stores the message passed as parameter as a class property. Now, if you turn your attention to the first class, you’ll notice it forces its $message incoming argument to be an object of type “Message.” If this condition isn’t satisfied, then the script will trigger a fatal error. To clarify this concept, study the example listed below:

$message=new Message('This is a test string');
$msglister=new MessageLister($message); // displays "This is a
test string';
$message='This is a test string';
$msglister=new MessageLister($message); // throws a fatal error.
$message is not of type 'Message'

In the first case, the above snippet will display the test string as one would normally expect, while in the second example, a fatal error is triggered, since the test string isn’t an object of type “Message.” That was simple, wasn’t it?

Right, I hope this basic example helped you to understand the functionality of “Type Hinting” in PHP 5. Now that you know how this feature works, let’s use it for improving the web page generator class that I showed you in my previous tutorial, so you can have gain an accurate idea of how “Type Hinting” can work for you in applications that use object type enforcing. Let’s move on together to learn how this is done.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: