Programming Tutorials

The Object (compound) Type in PHP

By: Jeffrey in PHP Tutorials on 2011-04-06  

Like every programming language, PHP offers the usual basic primitive types which can hold only one piece of data at a time (scalar). I am particularly fond of the "object" type (compound) because that allows me to group many basic PHP types together, and I can name it anything I want.

<?php
class Person
{
$firstName; // a PHP String
$middleName; // a PHP String
$lastName; // a PHP String
$age; // a PHP Integer
$hasDriversLicense; // a PHP Boolean
}
?>

Here, I have grouped several basic PHP types together, (3) Strings, (1) Integer, and (1) Boolean... then I named that group "Person". Since I used the proper syntax to do so, this code is pure PHP, which means that if you run this code, you would have an extra PHP "type" available to you in your scripts, like so:

<?php
$myAge = 16; // a PHP Integer - always available
$yourAge = 15.5; // a PHP Float - always available
$hasHair = true; // a PHP Boolean - always available
$greeting = "Hello World!" // a PHP String - always available
$person = new Person(); // a PHP Person - available NOW!
?>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in PHP )

The Object (compound) Type in PHP

Function to return number of digits of an integer in PHP

parent:: AND self:: in PHP

Exception in module wampmanager.exe at 000F15A0 in Windows 8

Building PHP with Apache2 on SuSE Professional 9.1/9.2

Floating point precision in PHP

Installing PHP with Apache 2.x on HP UX 11i and configuring PHP with Oracle 9i

Cannot load /usr/local/apache/libexec/libphp4.so into server: ld.so.1:......

Setting up PHP in Windows 2003 Server IIS7, and WinXP 64

error: "Service Unavailable" after installing PHP to a Windows XP x64 Pro

Running different websites on different versions of PHP in Windows 2003 & IIS6 platform

Installing PHP with nginx-server under windows

Convert IP address to integer and back to IP address in PHP

Function to force strict boolean values in PHP

Function to sort array by elements and count of element in PHP

Latest Articles (in PHP)