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
Category: PHP
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
Gazing into the Mirror of ‘self’ for Code Clarity This is how most books are built. Let’s use some keywords without explaining it to you previously. I was caught in that trap as well. We looked at the self keyword in the last couple of articles without actually going into detail as to what it means. https://www.dinocajic.com/php-static-keyword/ For static class members, we don’t use the $this keyword, we use self. That and the scope resolution operator allows us to access the static properties and methods. The self keyword does not require a dollar sign in front of it, unlike the $this keyword. The $ is pushed towards the static property, self::$property,
Unveiling Timeless Secrets within the Code Static methods are methods that belong to the class, and not to the object. Methods that belong to objects are sometimes referred to as instance methods. Similarly to how we accessed constants in the previous article without having to instantiate the class, we access static methods without instantiation also. https://blog.devgenius.io/php-p57-scope-resolution-operator-19c50ca607d4 To access those static methods, we’ll use the class name, followed by the scope resolution operator, and finally the static method name. ClassName::static_method() A little Computer Science jargon for you: static methods are stored in a separate portion of the heap memory. Instance methods
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
Piecing Together Masterpieces with Included Files Include, Require, Require Once, and Include Once. You might have seen these at the top of a file in someone else’s code, but what does each one of them mean? We’ll take a look at each later. For now, we’re just trying to split our file since it was quite huge last time. https://blog.devgenius.io/php-p54-inheritance-chain-3cb62edcb373 We want to start off slowly and separate each class into its own individual file. Each of those files are then included whenever necessary. For the most part, each of these keywords do the same function, which is to include
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.
