Cultivating Miniature Worlds through Sub-Namespace If you can define namespaces, then it’s no different to define sub-namespaces. We’re also going to look at classes with the same name from different sub-namespaces and how we’re going to handle those naming conflicts that still might arise. Recap: Review where we left off last time. The only difference is that we changed the namespace from CoolCars to Vehicles. If we start with our Car class, we can see that it belongs to the Vehicles namespace. <?php namespace Vehicles; require_once(‘Vehicle.php’); require_once(‘Engine.php’); require_once(‘Transmission.php’); class Car extends Vehicle { //… } What we want is for

Dividing and Conquering through Namespace Architecture Defining namespaces is pretty simple. At the top of the file, right after the opening PHP tag, the namespace keyword is used, followed by the virtual directory of your choosing. This virtual directory can act as a single directory, or be few sub-directories deep. We’re also going to be looking at using the use keyword. Remember the use keyword? Yeah, well it’s coming back. Recap: Review the previous article and code. We’ll be building off of our previous code. Let’s start off by creating our test file. <?php // Defining Namespaces require_once(“Lamborghini.php”); $lamborghini

Just a virtual directory trick Namespaces are used throughout numerous programming languages. If you’re familiar with Java, you might have heard of the term package. A package is a namespace in Java. The package allows the developer to group similar code together. In the simplest sense, a package is a folder or a directory. Namespaces give the programmer the ability to reuse class names in different contexts. The easiest way to look at namespaces from a non-programming perspective is through the convention on how people are named. My parents gave me the first name Dino. Although there are probably

Passing Down Constructor Legacies from Ancestors We’ve called the parent constructor in the last few articles without actually going into much detail about it. If the child class does not define a constructor, it inherits the parent constructor like any other method. If a child class defines a constructor of its own, the parent constructor will be overridden. To call the parent constructor, a call to parent::__construct() must be done from the child constructor. Recap: Review the previous article and code. Looking at the Lamborghini class, it’s just an empty class that extends Car. The Car class does contain

Pitting Code Titans Against Each Other PHP allows you to compare objects with the comparison and the identity operators. When using the comparison operator, PHP checks to see if two objects have the same attributes and values, and if they’re instances of the same class. With the identity operator, you check to see if the object variables point to the same instance of the same class. Recap: Review the code from the previous article. Aside from the Lamborghini class, we’re going to recreate the Ferrari class that we once had. <?php require_once(“Car.php”); class Lamborghini extends Car { }

Stamping Enduring Marks on Code’s Path with ‘final’ The final keyword can be prefixed to methods and classes. If you prefix a method in the parent class, and the child class inherits the methods from the parent, the final keyword prevents the child class from overriding that method. If the class itself is prefixed with final, then no other class can extend that class. Recap: Review where we left off in the previous article. The final keyword isn’t that complicated of a concept. We pretty much covered everything you need to know in the intro paragraph, but let’s go through

Conjuring Functionality Mystique with Anonymous Classes Anonymous classes are classes without a name. If you’ve been following this article series, this shouldn’t be that foreign of a concept to you. Think back on anonymous functions. An anonymous class can be assigned to a variable or be passed as an argument. They use the keyword Class and are instantiated with the new keyword. They can pass constructor arguments, inherit from their parent class, and implement interfaces. In other words, they can do everything that a regular class can do. Recap: Review the code from the previous article. We ended by creating the following

Conveying Insights by Passing Objects as Messengers We’ve looked at passing numerous different data types as arguments to methods, but we haven’t looked at passing objects as arguments to methods. We’ll type hint with the inherited class, which is an overview of polymorphism in PHP. We’ll also pass the object as an argument to the constructor. The constructor will then set the property inside the class. This is called dependency injection. We’ll explore both of these topics in detail later, but this is a good overview. Let’s continue on from where we left off in the previous article, with our

Serenading Complexity into Elegance with Harmonious Traits There is a fundamental problem in programming languages that support Multiple Inheritance. PHP is not one of those languages, but let’s walk through it anyways just so that you understand the issue. Assume there is a parent class that contains a specific method, such as drive. There are two children classes, class 1 and 2, and they each extend the parent class. They inherit the drive method and they override it. Now, PHP doesn’t support what I’m about to tell you since this is a multiple inheritance issue. Another class, class 3, extends

Building Worlds on Abstract Class Pillars Abstract classes  represent abstract concepts. In our previous article, we created various classes that inherited properties and methods from its parent. Looking at the Animal class hierarchy, the Animal class itself is an abstract concept. We can vaguely say Animal, but what type of animal? A Dog may be a narrow enough concept, but a GermanShepherd is definitely a concrete class, not abstract. Abstract classes are classes that start with the word abstract. If a class contains at least one abstract method, it must be declared as abstract. If a method inside of an