Transforming a script into a class enhances code modularity and reusability We covered file uploads in the last few articles, and purposefully strayed away from object oriented programming. Why? Because most of the time that you find a solution online to a problem like this, it won’t be laid out nicely for you. It’s going to be just a blob of code and not going to fit your architecture perfectly. I remember when I was a junior developer, that used to give me some anxiety. “Couldn’t they just have created this as a class instead of this script,” I found
Tag: PHP OOP
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. https://blog.devgenius.io/php-p69-defining-namespaces-5a8e0a4bfdc0 https://github.com/dinocajic/php-youtube-tutorials/tree/master 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. https://github.com/dinocajic/php-youtube-tutorials/tree/master https://blog.devgenius.io/php-p67-parent-constructor-ec8e41b9a6a9 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. https://blog.devgenius.io/php-p67-parent-constructor-ec8e41b9a6a9 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. https://blog.devgenius.io/php-p66-object-comparison-a2e84a8e3e13 https://github.com/dinocajic/php-youtube-tutorials/tree/master 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. https://github.com/dinocajic/php-youtube-tutorials/tree/master https://www.dinocajic.com/php-final-keyword/ 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. https://github.com/dinocajic/php-youtube-tutorials/tree/master https://blog.devgenius.io/php-p64-anonymous-classes-4d26c9e7281a 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. https://blog.devgenius.io/php-7-x-p39-anonymous-functions-75f6f99fa571 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. https://github.com/dinocajic/php-youtube-tutorials/tree/master 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. https://blog.devgenius.io/php-7-x-p36-function-arguments-f0b4c131ad1b 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