PHP basic OOP Constructor to Import
Learning from Udemy
Assalamualaikum warrahmatullahi wabarakatuh.
Constructor
Constructor , when we create an Object, we are like calling a function because we use brackets (), in a PHP class we can create a constructor. constructor is a function that
will be called the first time the Object is created like a function, we can pass parameters to the constructor, the name of the constructor in PHP must be __construct()
<?php
require_once "data/Person.php";
$anam = new Person("Choirul", "Cirebon");
var_dump($anam);
Destructor
Destructor is a function that will be called when the object is deleted from memory, when the object is no longer in use or when the application is about to die. To create a function destructor, we can use the function name __destruct specifically for the destructor. We may not add function arguments in everyday use, for example, it is suitable for closing connections to databases or close the process of writing to the file, so there is no memory leak.
<?php
require_once "data/Person.php";
$choirul = new Person("Choirul", "Cirebon");
$anam = new Person("Anam", "Semarang");
echo "Program Selesai" . PHP_EOL;
Inheritance
Inheritance is the ability to inherit from one class to another, meaning that we can create a Parent class and a Child class. Child class can only have one Parent class, but one Parent class can have many Child classes
when a class is derived, then all properties and functions in the Parent class will automatically be owned by the Child class. To inherit the Child class, we must use the extends keyword and then follow the name of the parent class.
void means not return value.
<?php
require_once __DIR__ ."/data/Manager.php";
$manager = new Manager();
$manager->name = "Choirul";
$manager->sayHello("Anam");
$vp = new VicePresident();
$vp->name = "Wakil President";
$vp->sayHello("Anam");
Namespace
when we are going to make an application, we can be sure that we will create a lot of classes, if there are too many classes, sometimes it will be difficult for us to find or classify the types of classes. PHP has a namespace feature where we want to access the classes contained in the namespace we need to name the namespace. Namespace is good when we have several classes in common using the same class name namespace or not will be an error in PHP. to create a namespace we can use the keyword namespace if we want to create a sub namespace we simply use the \ character after the previous namespace
Functions and Constants in the namespace
in addition to classes we also use functions and constants in the namespace, and if we want to use these functions or constants we can use them by starting with the namespace name.
<?php
require_once "data/Conflict.php";
require_once "data/Helper.php";
$conflict = new \Data\One\Conflict();
$conflict = new \Data\Two\Conflict();
echo Helper\APPLICATION . PHP_EOL;
Helper\helpMe();
Global Namespace
by default when we code in PHP it is actually stored in the global namespace. A global namespace is a namespace that does not have a namespace.
use Keyword
previously we already know that to use a class, function or constant in a namespace we need to mention the namespace name at the beginning if we use the same class, function or constant too often, there will be too much duplication by calling the same namespace many times, we can do this avoid by importing the class, function or constant using the use keyword.
Alias
when we use use it means that we no longer need to use the namespace at the beginning of the class when we want to create the class, but what if it turns out that the class name is the same? Therefore, in PHP there is an Alias feature, Alias is the ability to create another name for an existing class, function or constant, using the as keyword after use
<?php
require_once "data/Conflict.php";
require_once "data/Helper.php";
use Data\One\Conflict as Conflict1;
use Data\One\Conflict as Conflict2;
use function Helper\helpMe as help;
use const Helper\APPLICATION as APP;
$conflict1 = new Conflict1();
$conflict2 = new Conflict2();
help();
echo APP . PHP_EOL;
Use group declaration
sometimes we need to import many things in the same namespace PHP has a use group feature, where we can import multiple classes, functions, or constants in one use command
to do that we can use brackets().
<?php
require_once "data/Conflict.php";
require_once "data/Helper.php";
use Data\One\Conflict as Conflict1;
use Data\One\Conflict as Conflict2;
use function Helper\helpMe as help;
use const Helper\APPLICATION as APP;
$conflict1 = new Conflict1();
$conflict2 = new Conflict2();
help();
echo APP . PHP_EOL;
That for today, thanks
Wassalamualaikum warahmatullahi wabarakatuh.