Designing Elegant Solutions through Interface Magic The topic of interfaces. An interface resembles an abstract class but there are a few differences. With the abstract class, you can create method declarations and implemented methods. Child classes can then inherit an abstract class and override or implement its methods. With an interface, you can only define the functionality. You can’t actually implement anything. The biggest difference is that a child class can only extend one abstract class but it can implement multiple interfaces. You will hear the term contract when talking about interfaces meaning that once you implement it, you are obligated

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. https://www.dinocajic.com/php-self-keyword/ 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

Ascending Logic Peaks with the Resolution Operator In PHP, the scope resolution operator, or the double colon ::, allows you to access constants, static properties and methods, and overridden properties and methods. We’ve created a couple of constants in our previous articles, but we’ve never actually accessed them. Method Overriding If you’re accessing, for example, a constant outside of the class, you would append the double colon operator to the instantiated object, or even the class name. If you’re accessing the constant or overridden property or method inside of the class, you would use either self, parent, or the static keyword. We’ll cover self and parent in this article;

Breathing Fresh Life into Logic with Method Overrides PHP’s object oriented principles allow for both method overloading and method overriding. You might have heard both of those terms, but don’t know what they mean, so I want to help to differentiate them for you. Method overloading is when you have two methods with the same name but a different number of parameters. In programming languages like Java, this is pretty simple to achieve. You create two methods with the same name and you provide them with a different number of arguments. area(diam) area(length, width) In the example above, we have

Tracing the Lineage of Functionality in Inheritance Chains In the last article I mentioned briefly that you can’t extend multiple classes at once. But the parent class can itself be a child class and extend a class of its own. In this article, we’ll be cleaning up our Dog class. We’ve pretty much moved everything from the GermanShepherd class into the Dog class, but there are a few properties and methods that can venture even further up to Mammal and Animal classes. Before we get into that, let’s review Inheritance with the Car class that we didn’t cover in the

Passing Down Code Wisdom through Generations Who would have guessed that PHP supports inheritance? Everyone using PHP since Object Oriented Programming became a thing, that’s who. If you’re not familiar with inheritance in programming, it’s just mimicking real world concepts, primarily ancestral inheritance. You inherit traits or characteristics from your parents, your parents from their parents and so on. You and I share the same traits as all human beings, so our parent class can be Human. In that Human class we can list methods like walking and speaking. Even though we speak different languages, the mechanism is the same.

Safeguarding and Sculpting with Getters and Setters In the last article, we covered visibility modifiers and set all of our properties to private. https://www.dinocajic.com/php-visibility-modifiers/ If we wanted to access those properties or modify them in their current state, we wouldn’t be able to. That’s where getters and setters, or accessors and mutators, come into play. Getters and setters are just the public methods that either modify or retrieve a property. With the various setter methods, we can use checks to validate the submitted data. For example, let’s say that we were modifying the $year property with our setter. The argument that’s received by the setter

Navigating Logic’s Shadows with Visibility Keys It seems like I’ve mentioned visibility modifiers in most of the previous articles; it’s finally time to tackle them. We’ll continue where we left off with our classes in the previous article. PHP — P50: Destructors Up until now, we’ve used the public visibility modifier for each of our properties and methods; that just means that everyone has access to it. You can access them inside and outside of the object. The default visibility modifier is public, which means that if we created any of the properties or methods and omitted the visibility modifier, PHP would automatically

Ending Chapters in Code with Destructive Grace Destructors are the opposite of constructors…go figure. The destructor method is called after the object is not referenced any more. In PHP, the constructor is called first, followed by any internal methods that the user accesses, followed by the destructor method for final cleanup. The destructor is even called if the PHP script is exited using PHP’s exit() function. https://www.dinocajic.com/php-constructors/ To create a destructor method, we’ll follow a similar naming convention to the constructor method. You start with two underscores, followed by the keyword destruct: __destruct(). <?php class ClassName { //… public function __destruct() { //…

Erecting Worlds from Blueprints with Constructors If you’re used to other object oriented programming languages, you’re probably familiar with the constructor. You may also be familiar with the constructor being named as the class name; it’s just a method that shares the same name as the class name. In PHP, it’s actually simplified in my opinion. You name the constructor method, construct with two underscores before it: __construct(). Great, but what is the point of the constructor. It’s there to initialize properties inside the object upon instantiation; PHP calls the constructor method automatically when the object is instantiated. Let’s look at some code to see