Concept & Definitions

Concept Definition
NAMESPACE Namespace solves name collisions between your code and PHP/External librarys' classes, functions and constants.
  • It is a string words separated with backslash
  • It actually updates the class name
  • Once introduced, class will be identified as Namespace + \ + Class
  • To use class now, you need to write ‘Namespace + \ + Class’
  • Shortcut to above is using ‘use’ statement
# Example PHP file.
namespace My\Namespace;

class MyClass {
  public static function myMethod() {
    ..
  }
}

# Calling myMethod before introducing namespace.
MyClass::myMethod()

# Calling myMethod after introducing namespace.
My\Namespace\MyClass::myMethod()

USE STATEMENT use Statement imports the specified namespace (or class) to the current scope.
  • It is class references for the shortcut class-name used in the file
  • When PHP encounters shortcut class-name in file, it checks all (end of the) use statements if it has the references
  • If u miss using using correct use statement, PHP will try to load that class from namespace mentioned in current file
  • It does not include anything, read collection
# Example PHP file.
namespace My\Namespace;

use OtherNamespace\MyClass1;
use OtherNamespace\MyClass2 as MyAlias;

# PHP internally converts.
MyClass1::myMethod(); to OtherNamespace\MyClass1::myMethod();
MyAlias::myMethod(); to OtherNamespace\MyClass2::myMethod();
UnknownClass::myMethod(); to My\Namespace\UnknownClass::myMethod();

ENCAPSULATION
With respect to a class encapsulation refers to the wrapping of data with the methods that operate on that data.
This way data is correctly accessed in a certain way, typically, only the object's own methods can directly inspect or manipulate its data, consequently one cannot randomly change the aspect of the program.

Example, a person's email can only be set via a setEmail($email) method.
The method may contain validation for email so that accidently one just cannot set any value for email, say
$user->email = 'myemail.com';
It could lead to some logical issue in the system possibly failure in sending system generated emails.
SELF (Keyword)
self (also 'parent' and 'static') keyword is used to access properties or methods within the class definition.
Sometimes, late static binding is required where self keyword is ignored; learn static keyword.

class Father {
  public function sayHello() {
    echo self::class . ' said Hello!'; // Is supported by ClassName::class magic constant.
    // This is similar to __CLASS__ . ' said Hello!';
  }

  public function getAge() {
    return $this->age;
    // self->age will throw error as Father->age holds no sense.
  }

}

$father = new Father();
$father->sayHello(); // Father said Hello!
STATIC (Keyword)
By declaring methods or properties static you can access them (via Scope Resolution Operator (::)) without needing the class object. Consequently, the pseudo-variable $this is not available within the methods declared as static.
ClassName::methodName();

Note: Object operator (->) does not work $father->getGender();
static (also 'self' and 'parent') keyword is used to access properties or methods within the class definition.
Additionally, static keyword resolves the limitation of referencing the class that was initially called at runtime, this is also called Late Static Binding.
Fact: It was decided not to introduce a new keyword but rather use static that was already reserved.

self:: says - I will execute in the context of the class whom I physically belong to.
Whereas, static:: says - I will execute in the runtime context.


Limitation,

class Father {
  public static $temp = 100;
  private static $gender;

  public function sayHello() {
    echo static::class . ' said Hello!';
    echo self::class . ' said Hello!';
  }

  public function saySorry() {
    echo self::class . ' said Sorry!';
    echo static::class . ' said Sorry!'; // Here comes the Late Static Binding.
  }

  public static function getGender() {
    return self::$gender;
    // Important: $gender must be a static property.
    // Accessing $gender via $this ($this->gender) is invalid.
  }
}

Class Son extends Father {
  public function sayHello() {
    echo static::class . ' said Hello!';
    echo self::class . ' said Hello!';
    // This is similar to __CLASS__ . ' said Hello!';
  }
}

$son = new Son();
echo $son::$temp;
// 100
$son->sayHello();
// Son said Hello!
// Son said Hello!
$son->saySorry();
// Parent said Hello!
// Son said Hello!

Comments

  1. UUID - Universally Unique Identifier

    ReplyDelete
  2. Polymorphism allows different types of objects to pass through the same interface.

    https://www.techtarget.com/searchapparchitecture/definition/object-oriented-programming-OOP#:~:text=Polymorphism%20allows%20different%20types%20of%20objects%20to%20pass%20through%20the%20same%20interface.

    ReplyDelete

Post a Comment

Drupal Contribution
Git Commands
RESTful Services
Lando Commands
Docker Commands
MySQL
Database Quick Code
Drush Commands
Drupal Console
PHP Quick Code
Drupal Quick Code
Composer Commands
Linux Commands
Linux Shell Scripting
Drupal Hooks
Twig Tricks
PHPUnit Test
PhpMyAdmin
Drupal Constants
CSS Clues
BLT Commands
Vagrant Commands
Localhost
127.0.0.1
Drupal Interview
Drupal Certifications
Concept & Definitions
Mac Tips
Windows Tips
Browser Tips

Best Practice

Use 'elseif' instead of 'else if'
#CodingTips

As of PHP 5.4 you can also use the short array syntax, which replaces array() with []
#CodingTips

Functions in general shall be named using snake_case(say, my_function()), and using camelCase(say, myFunction()) when declared within a plugin class
#CodingTips

Variables in general shall be named using snake_case(say, $my_variable), and using camelCase(say, $myVariable) when declared within a plugin class
#CodingTips

Manage automatically assigning of new permissions whenever a module is enabled here- admin/config/people/accounts
#ConfigurationTips

Manage source of Main-menu and User-menu links here- admin/structure/menu/settings
#ConfigurationTips

Helper function(s) shall be named prefixing an underscore(say, _my_helper_function()), which can prevent hooks from being called
#CodingTips

Ideally, configuring of 'Private file system path' at admin/config/media/file-system should be located outside of your Drupal root folder(say, ../my_private_files)
#ConfigurationTips

You should be aware that uploading files as 'Private file' will slow down the process of loading the files as Drupal has to be bootstrapped for every file that needs to be downloaded
#ConfigurationTips #BeAware

Code should always be pushed up(dev -> staging -> production) and databases should only be pushed down(production -> staging -> dev)
#DevelopmentTips

Get Raw SQL Query of drupal dynamic queries before executing it using $query->__toString();
#DebugTips

In VI-Editor, Press ESC key to come in command mode and for undo type :U and for redo type :Ctrl+R
#LinuxTips

Insert queries must always use a query builder object(layer of abstraction), allowing individual database drivers special handling for column values (if applicable), example case for LOB and BLOB fields.
#DatabaseQueryTips

Drupal uses the .inc extension to prevent files from being executed directly.
#DevelopmentTips

Popular Posts