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

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

  Code Real Estate: Where Properties Define Object Value Properties, also called Class Member Variables, Attributes, and Fields, are the characteristics of a class. Imagine them as variables placed inside of the class’s curly braces, but not inside of methods themselves. Properties can be directly initialized or they can be initialized when the object is instantiated through the constructor; we’ll get to constructors later. Properties can have different visibility based on the keyword in front of them: public private protected Visibility will also be explored later, but for the time being, private properties are only visible to the object, public properties are visible

Using Class Blueprints to construct real objects PHP has been an Object Oriented Language since PHP 5. It’s mature, powerful, and there should be no reason for anyone to hate on it any longer. In this article, I go over the differences between objects and classes, do a short introduction to object oriented programming concepts, and create a small introductory class showing class properties, methods, and local variables. Classes vs Objects Imagine that you’re constructing a building. Usually the architect, you as a programmer, will develop a blueprint, the class, and from that blueprint, a building will be constructed, the

Embarking on Adventures in the Land of Logic It’s time for callback functions. What is a callback function? It’s just a function that’s passed as an argument to another function and is then executed inside the other function. I think it’s time to go through this concept in excruciating detail to put this behind us once and for all. We’ll cover passing anonymous functions (closures) and arrow functions as arguments to other functions, and then calling them too. Callback functions make a lot of sense in asynchronous languages since we may not want two functions to compete for finishing times.