How to Build an Object in PHP with Object-Oriented Programming - dummies

How to Build an Object in PHP with Object-Oriented Programming

By Andy Harris

The PHP mechanisms for object-oriented programming (OOP) are important to learn because many of the advanced libraries you’re likely to use are object-oriented, and because properly implemented OOP can tame complex programs in a big way.

Start your experiments in OOP by looking over a simple PHP file:

<?php
//SimpleCritter.php
//meant to be included
class Critter{
 public $name;
 public function __construct($name = "Anonymous"){
 $this->name = $name;
 } // end constructor
 public function sayHi(){
 return "Hi. my name is $this->name.";
 } // end sayHi method
} // end critter def
?>

This is an interesting PHP file because it doesn’t follow the patterns you’ve seen before. This code isn’t meant to be run directly, but to be reused by other code. Here are the highlights:

  1. No HTML needed here.

    This file is pure PHP. It doesn’t need any HTML at all because it will be called by another PHP program. Code reuse is the goal here, so this is code designed to be reused.

  2. Define a class.

    Use the class keyword to define a class (that is, the recipe for making the object). In this example, the Critter class is being defined. Note that class names are typically capitalized.

  3. Define a property.

    If you define a variable inside a class, it becomes a property. Properties are much like variables, but they live inside a class. The keyword public indicates that the variable will be available to any code that wants it. Properties are the characteristics of an object.

  4. Define a method.

    Skip ahead to the sayHi()function. For the most part, it looks just like any other function. But when a function is defined inside an object, it becomes a method. Methods are things the object can do. Most methods are declared public. Methods, like other functions, can have parameters and return values.

  5. Use $this to refer to the current object.

    Within an object definition, the special keyword $this refers to the object currently being defined. The $this keyword is normally used to differentiate properties and methods from ordinary variables and functions.

  6. $this->name refers to the name property.

    The special symbol ->is a dereference operator. Really that’s fancier than it sounds. It simply indicates that name is part of the object.

  7. Build a constructor.

    In addition to ordinary methods, objects can have a special method called a constructor. In PHP, the constructor is called __constructor (with two preceding underscores). Constructors are special functions that are automatically called when a class is being instantiated.

    Constructors are normally used to initialize all the properties and set up any housekeeping that might be necessary when a new instance of the class is being created. Traditionally, the constructor is listed as the first method in the class even if it isn’t always written first.

  8. The constructor takes a parameter.

    Like any function, a constructor can take one or more arguments. In this case, you want the option to name a critter as soon as it’s built, so the constructor has a $name parameter.

  9. The parameter has a default argument.

    If the user doesn’t specify a parameter, the constructor will assign “Anonymous” as a default value.

  10. End the class definition.

    The entire class definition goes inside a pair of squiggly braces, so don’t forget to indent your code and comment on end quotes so it’s clear what you’re ending.