Jul 14, 2024
class Person {
public $name;
public $address;
public $country = 'Indonesia';
}
$person1 = new Person();
$person1->name = 'Eko';
$person1->address = 'Subang';
class, format CamelCase:: tanpa membuat objek terlebih dahuluclass MathHelper {
public static function sum($a, $b) {
return $a + $b;
}
}
$result = MathHelper::sum(1, 2); // Output: 3
extendsclass Manager {
public $name;
public function sayHello($name) {
echo "Hello $name, my name is $this->name";
}
}
class VicePresident extends Manager {
}
$manager = new Manager();
$vp = new VicePresident();
$manager = $vp; // Polymorphism
use keywordclass ValidationException extends Exception {
}
try {
throw new ValidationException("Error!");
} catch (ValidationException $e) {
echo $e->getMessage();
} finally {
echo "Done.";
}
$reflectionClass = new ReflectionClass('Person');
$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
echo $property->getName();
}
preg_match, preg_replace, preg_splitif (preg_match('/Eko|awan|Ed/i', 'Eko Kurniawan Kennedy', $matches)) {
print_r($matches);
}
format, setDate, setTime, etc.$date = new DateTime();
$date->setDate(1990, 1, 20);
$date->setTime(10, 10, 10);
echo $date->format('Y-m-d H:i:s'); // Output: 1990-01-20 10:10:10
Further Learning:
Referensi Tambahan: