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,
Author: Dino Cajic
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.
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() { //…
