How-to articles, tricks, and solutions about OOP

Creating anonymous objects in php

In PHP, anonymous objects can be created using the new class syntax.

Difference between @staticmethod and @classmethod

In Python, a method is a function that is associated with a class.

Examples of GoF Design Patterns in Java's core libraries

There are many examples of the GoF (Gang of Four) design patterns in the core libraries of Java. Here are some examples:

Get PHP class property by string

In PHP, you can use the $ operator to access an object's properties by name.

How should I have explained the difference between an Interface and an Abstract class?

An interface is a collection of abstract methods that define a set of functions that a class must implement.

Interface or an Abstract Class: which one to use?

An interface defines a set of methods that a class must implement, but does not provide any implementation for those methods.

Meaning of @classmethod and @staticmethod for beginner

In Python, @classmethod is a decorator that is used to define a method as a class method.

Multiple Inheritance in PHP

In PHP, multiple inheritance is not supported in the traditional sense, where a class can inherit from multiple classes.

Type hinting in PHP 7 - array of objects

In PHP 7, type hinting can be used to specify the expected data type of a function or method parameter.

Understanding Python super() with __init__() methods

The super() function is a way to refer to the parent class and its attributes.

unexpected 'use' (T_USE) when trying to use composer

This error message is indicating that there is an unexpected use of the keyword "use" in your code when trying to run Composer.

Usage of __slots__?

__slots__ is a way to specify a fixed set of attributes for a class, which can help to save memory in certain situations.

Using $this inside a static function fails

Using $this inside a static function is not allowed in PHP because the $this variable is only available within the context of a non-static method.

What are metaclasses in Python?

In Python, a metaclass is a class that defines the behavior of a class. When you create a class, Python automatically creates a metaclass for you behind the scenes. You can think of a metaclass as a blueprint for creating a class.

What are the differences between type() and isinstance()?

In Python, type() is a built-in function that returns the type of an object, while isinstance() is a function that checks whether an object is an instance of a particular class or of a subclass thereof.

What do __init__ and self do in Python?

__init__ is a special method in Python classes, also known as a constructor.

What does the 'static' keyword do in a class?

In a class, the static keyword is used to declare a static member, which belongs to the class itself rather than an instance of the class. This means that you can access a static member without creating an instance of the class.

What is a mixin and why is it useful?

A mixin in Python is a class that is used to add specific functionality to other classes without inheriting from them.

What is the difference between public, private, and protected?

In PHP, public, private, and protected are access modifiers that control the visibility of class properties and methods.

What is the meaning of single and double underscore before an object name?

In Python, a single underscore "_" before an object name indicates that the object is meant to be private, meaning that it should not be directly accessed or modified outside of the class that it is defined in.

What is the purpose of the `self` parameter? Why is it needed?

The self parameter in Python is used to refer to the instance of an object within a class.

Why do Python classes inherit object?

In Python 3, all classes automatically inherit from the object class.

Why use getters and setters/accessors?

Getters and setters, also known as accessors, are methods that are used to get and set the values of an object's properties.