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
Tag: OOP
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
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
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
Building Strong Foundations through Declarations If you come from the olden days of PHP, you might have been spoiled by having PHP automatically declare types for you. The convenience is not something you want every time. Sometimes you want to be able to specify what type of data you want to store or return. It is still optional in PHP; it’s up to you whether you want to use it or not. If you do use it, which I encourage you to do so, and you specify the wrong data type, PHP will throw an error. As of PHP 7,
Empowering Objects to Speak with ‘this’ Voice In the previous articles, we’ve seen how we can access the methods inside of the object using the object operator. This is done when an object is instantiated. We’ve already created a Car object and accessed the drive() method. You did this with the object operator, ->, after instantiating the object: $lamborghini->drive(); <?php Class Car { // … public function drive( $is_car_on = false ) { if ( $is_car_on ) { echo “I’m driving”; } else { echo “You gotta turn me on”; } } } $lamborghini = new Car(); $lamborghini->color = “Black”; $lamborghini->make = “Lamborghini”; $lamborghini->model
Conducting Code Harmony through Expert Methods What is a method? In PHP, it’s just a function inside of a class. That’s the easiest way to think about it. You initialize them the exact same way that you would a function, including using the function keyword. PHP – P35: User Defined Functions In object oriented programming, objects can have specific characteristics, but they can also perform specific actions. The actions are represented by methods. Methods can also have visibility modifiers, such as private, protected, and public, but we’ll leave those out of this article and stick to the default public modifier. We’re building on from our previous article and
Keeping Code Grounded in a Sea of Variables Class constants are similar to regular constants with the exception that they live inside the class. Class constants cannot be changed, hence the name constant. They’re declared using the const keyword and are allocated in memory once per class and not each time that you instantiate the object; this is what’s meant when you hear that constants live within the class. They resemble static variables in that sense, but static variables can be modified. To access the class constant, you need to use the class name and the scope resolution operator (double colon). Constants do not