This page containt guide or general information based on best practices for code in PHP for developer , this note dedicated for other developer so able to learn PHP with right way. in this section is shared about fundamental progamming with php .
This page was guided by these :
Here the simplest explanation about these concept :
Code Example :
<?php
class Product {
public $judul = "judul",
$penulis = "penulis",
$harga = 0;
public function __construct($judul,$penulis,$harga){
$this->judul = $judul;
$this->penulis = $penulis;
$this->harga = $harga;
}
public function getLabel(){
return "$this->judul, $this->penulis";
}
}
class PrintInfoProduct {
public function doPrint( Product $product ){
$string = "Nama komik , Nama , Harga";
return $string;
}
}
$product1 = new Product('Naruto','Masusi Natsumoto',80000);
$infoProduct = new PrintInfoProduct();
echo $infoProduct->doPrint($product1);
Note:
public function doPrint( Product $product )
is class object type , its possible to set parameter as object . read more about it will lead in dependency injection, polymofsm.
This function will automatically called when first create or inisiate a class. you can set this by adding __ (2 underscores) :
<?php
class Product {
public $judul = "judul",
$penulis = "penulis",
$harga = 0;
public function __construct($judul,$penulis,$harga){
$this->judul = $judul;
$this->penulis = $penulis;
$this->harga = $harga;
}
public function getLabel(){
return "$this->judul, $this->penulis";
}
}
$product1 = new Product('Naruto','Masusi Natsumoto',80000);
echo $product1->getLabel();
Inheritance is relational between two or more classes where derived class inherits of properties of pre existing (base) classes
Base Classes : it is a class who inherited by another class , also called parent class or super class
Derived Class : it is class who inherit properties by parent (base) class , also called child class.
class Animal {
public function makeSound() {
echo "The animal makes a sound.";
}
}
class Dog extends Animal {
public function makeSound() {
echo "The dog barks.";
}
}
// Create an object of the Dog class
$dog = new Dog();
// Call the makeSound() method on the Dog object
$dog->makeSound(); // Output: The dog barks.
There also more principlies in to discover more , well available on full source code above