PHP, Object-Oriented Programming [Part 1]

PHP, Object-Oriented Programming [Part 1]
Reading time: 3 min read
Link copied!

Greetings!

It’s with great enthusiasm that I start a series of posts about Object-Oriented Programming with PHP.

Recently I received from my wonderful girlfriend an excellent book: PHP, Programming with Object Orientation, by Pablo Dall’Oglio.

The book is so fantastic that I devoured it in just 15 days. No, it’s not a small book! It has almost 600 pages in a very didactic way, always containing a practical example for each explanation.

I’ll make a series of posts, featuring the book topics that I find most relevant. Of course I won’t make exact explanations like the book, but rather a broader approach.

I recommend the book to all programmers, both beginners and more experienced. Without a doubt it’s a reading that’s very worthwhile.

1. What is PHP?

According to Wikipedia: The language emerged around 1994, as a package of CGI programs created by Rasmus Lerdorf, with the name Personal Home Page Tools, to replace a set of Perl scripts he used in developing his personal page.

PHP Evolution
  • 1997: PHP/FI with Forms Interpreter
  • PHP 3: First object orientation resource
  • PHP 4: More power to the machine, more OOP resources
  • PHP 5: Works with handlers, solves object copy problems
1.1 File Extensions

PHP files generally follow these patterns:

  • .php, File containing a PHP program
  • .class.php, File containing a class in PHP
  • .inc.php, Files with configuration parameters
2. Code Delimiters

To write PHP code, we start with delimiters:

<?
CODE
?>

OR

<?php
CODE
?>

At the end of each command it’s finished with a semicolon ;.

3. Comments

Single Line Comments

<?
// comment
?>

or

<?
# comment
?>

Block Comment

<?
/* comments
   comments ..
*/
?>
4. Output Commands

echo

<?
// echo prints one or more variables
echo 'Hello world';
?>

print

<?
// print prints a string
print 'Hello world';
?>

var_dump

<?
// var_dump, widely used to debug an array
var_dump($array);
?>

print_r

<?
/* print_r prints variables in an
explanatory way, but in a more readable manner
*/
print_r($array);
?>
5. Variables

Variables are identifiers with volatile and mutable values, which only exist during program execution. A variable is always preceded by the $ (dollar sign) character.

<?
$fruit = "Avocado";
echo $fruit; //Result: Avocado
?>
Best Practices for Naming
  • Use descriptive names: $studentName instead of $n
  • Don’t start with numbers or special characters
  • Use camelCase: $productName
  • PHP is case-sensitive: Avocado ≠ avocado
5.1 Variable Types

Boolean Type

<?
// Variable receives true value
$showFruit = TRUE;
if($showFruit){
    echo 'Avocado';
}
?>

Numeric Type

<?
// decimal number
$a = 1234;
// a negative number
$a = -123;
// octal number (equivalent to 83 in decimal)
$a = 0123;
// hexadecimal number (equivalent to 26 in decimal)
$a = 0x1A;
// floating point
$a = 1.234;
// scientific notation
$a = 4e23;
?>

String Type

<?
$fruit = 'Avocado';
$fruit = "Avocado";
?>

Array Type

<?
$fruits = array('Avocado', 'Pineapple', 'Plum');
echo $fruits[0]; // Result: Avocado
?>

Object Type

<?
class Computer{
    var $cpu;
    function turnOn(){
        echo "Turning on computer at {$this->cpu}...";
    }
}
$obj = new Computer;
$obj->cpu = "500Mhz";
$obj->turnOn(); // Result: Turning on computer at 500Mhz
?>
6. Constants

A constant is a value that doesn’t change during program execution. Usually uppercase names are used.

<?
define("MAX_CLIENTS", 100);
echo MAX_CLIENTS; // Result: 100
?>

That’s all for today folks.

We’ll be back…