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 )

PHP code to write to a CSV file from MySQL query

Different versions of PHP - History and evolution of PHP

PHP code to import from CSV file to MySQL

Send push notifications using Expo tokens in PHP

Encrypting files using GnuPG (GPG) via PHP

PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......

A Basic Example using PHP in AWS (Amazon Web Services)

Decrypting files using GnuPG (GPG) via PHP

Parent: child process exited with status 3221225477 -- Restarting

Error: Length parameter must be greater than 0

Password must include both numeric and alphabetic characters - Magento

Convert a hex string into a 32-bit IEEE 754 float number in PHP

Convert XML to CSV in PHP

Count occurrences of a character in a String in PHP

PHP file upload prompts authentication for anonymous users

Latest Articles (in PHP)