PHP Objects and Classes

Using Class Blueprints to construct real objects

PHP has been an Object Oriented Language since PHP 5. It’s mature, powerful, and there should be no reason for anyone to hate on it any longer. In this article, I go over the differences between objects and classes, do a short introduction to object oriented programming concepts, and create a small introductory class showing class properties, methods, and local variables.

Classes vs Objects

Imagine that you’re constructing a building. Usually the architect, you as a programmer, will develop a blueprint, the class, and from that blueprint, a building will be constructed, the object. When you create a class, you’re developing the schematics. When the program runs, it looks at those schematics (the class) and constructs the object (instantiates the object).

  • Architect = Programmer
  • Blueprint = Class
  • Building = Object
  • Instantiate = Convert from Class to Object

What we’re doing with a program most of the time is mirroring real-world things. The object that’s constructed from a class doesn’t have to be non-living; we can model living organisms or any idea that can represent an object. Thinking about an animal, like a Dog, we can say that a dog has characteristics, such as: breed, height, length, fur color, etc. We don’t have to just model characteristics of the Dog; we can also model actions that the Dog can perform, such as: barking, walking, drooling, etc. We’ll look at the Dog example in the next few articles, but right now let’s take a look at the class structure.

Class Structure

The class will start with the keyword class and is followed by the Class Name. All characteristics (properties) and actions (methods) will go in between the curly braces.

<?php

class ClassName {
    // Properties
    // Methods
}

?>

The property starts off with the visibility modifier, public, private, or protected, followed by the property name. We’ll be looking at visibility modifiers in a later article. Property names can be initialized or set later on. Data types can be specified as of PHP 7.4, such as string, int, etc. If you’re familiar with programming languages like Java, you’ve been exposed to data structures. Explicitly stating data types is a relatively new concept for the PHP world. We’ll discuss that topic in a later article as well.

<?php

class ClassName {
    
  public $property_name = "";        // initialized to empty string
  private $property_name_2 = "Dino"; // initialized to string
  protected $property_name_3 = 19;   // initialized to int
  
  public $property_name_4;           // not initialized
}

?>

Another topic that will be discussed in greater depth later will be the constructor. The constructor will be loaded during object instantiation.

<?php

class ClassName {
    
  public function __construct() {
    // Called when class is instantiated
    // $object_name = new ClassName();
  }
}

?>

The constructor looks like a function, because technically it is. A function inside of a class is called a method. The constructor is a special type of method. Methods (actions) inside of the class start with the visibility modifier, public, private, protected, and are declared the same way as a function. Methods can have parameters that can be accessed from within the method. Methods can also have local variables that can only be accessed by the methods itself.

Properties have class scope and can be accessed by any method. To access the properties, or other methods, from within the method, you have to use the $this keyword. You’re just telling PHP that you’re accessing the property or method of this object. If you’re accessing the property or method of another object, you would instantiate that object and use the name to access it. After using the $this keyword, you use the object operator (->) to retrieve the property or the method of the object.

<?php

class ClassName {
  
  public $property_name = "";
    
  public function someMethod( $parameter ) {
    
     $local_variable = "";
  }
  
  public function anotherMethod() {
    
    echo $this->property_name;
    $this->someMethod("Hey");
  }
}

?>

To instantiate the object, you have to use the new keyword. After the new keyword, you will specify the class name and append the opening and closing parentheses to the name. The instantiated object will be assigned to a variable. Once the object is instantiated, you can access the public methods and properties with the object operator, ->.

<?php

class ClassName {
  
  public function hello() {
    echo "Hey";
  }
  
}

$object_var = new ClassName();
$object_var->hello();

?>

Let’s look at what happens in the code above.

  1. We create a class (blueprint) and name it ClassName.
  2. We specify a single method inside of the class, hello(). The method will just echo out Hey once it’s called. We set the visibility modifier to public letting PHP know that the method can be called outside of the class.
  3. The blueprint is constructed into an object on line 11. In other words, the object is instantiated and assigned to $object_var.
  4. Once the object is instantiated, we have access to methods and properties inside of the object. The hello() method is visible and is called with the aid of the object operator (->) on line 12.
  5. The hello() method echoes out Hey.

We’ll be looking at each of these concepts in greater detail in the following articles. If Object Oriented Programming is not something that you’re familiar with, I highly recommend that you get acquainted with the concept. Here’s a pretty good article if you’re new to the concept.

Continue your learning with these articles

PHP Callback Functions

EMBARKING ON ADVENTURES IN THE LAND OF LOGIC

PHP – P42: CALLBACK FUNCTIONS

What is a callback function? It’s just a function that’s passed as an argument to another function and is then executed inside the other function.

PHP Objects and Classes

Using Class Blueprints to construct real objects

PHP – P43: objects and classes

PHP has been an Object Oriented Language since PHP 5. It’s mature, powerful, and there should be no reason for anyone to hate on it any longer.

PHP Class Properties

CODE REAL ESTATE: WHERE PROPERTIES DEFINE OBJECT VALUE

PHP – P44: CLASS PROPERTIES

Properties, also called Class Member Variables, Attributes, and Fields, are the characteristics of a class. Imagine them as variables placed inside of the class’s curly braces, but not inside of methods themselves.

Leave a Reply