├── .gitignore ├── AbstractClass.php ├── AbstractFunction.php ├── AnonymousClass.php ├── ComparingObject.php ├── Constant.php ├── Constructor.php ├── Covariance.php ├── DateTime.php ├── DebugInfo.php ├── Destrcutor.php ├── Exception.php ├── Function.php ├── FunctionOverriding.php ├── Generator.php ├── GetterAndSetter.php ├── Import.php ├── ImportAlias.php ├── ImportGroup.php ├── Inheritance.php ├── Interface.php ├── Invoke.php ├── Namespace.php ├── Object.php ├── ObjectCloning.php ├── ObjectIteration.php ├── Parent.php ├── Polymorphism.php ├── Properties.php ├── PropertiesOverloading.php ├── Reflection.php ├── RegularExpression.php ├── Static.php ├── StdClass.php ├── ToString.php ├── Trait.php ├── TraitConflict.php ├── Visibility.php ├── data ├── Animal.php ├── AnimalShelter.php ├── Car.php ├── Category.php ├── Conflict.php ├── Food.php ├── Helper.php ├── Location.php ├── LoginRequest.php ├── Manager.php ├── Person.php ├── Product.php ├── Programmer.php ├── SayGoodBye.php ├── Shape.php ├── SocialMedia.php └── Student.php ├── exception └── ValidationException.php └── helper ├── MathHelper.php ├── Validation.php └── ValidationUtil.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml -------------------------------------------------------------------------------- /AbstractClass.php: -------------------------------------------------------------------------------- 1 | name = "Luna"; 9 | $cat->run(); 10 | 11 | $dog = new Dog(); 12 | $dog->name = "Doggy"; 13 | $dog->run(); 14 | -------------------------------------------------------------------------------- /AnonymousClass.php: -------------------------------------------------------------------------------- 1 | name = $name; 14 | } 15 | 16 | public function sayHello(): void 17 | { 18 | echo "Hello $this->name" . PHP_EOL; 19 | } 20 | }; 21 | $helloWorld->sayHello(); 22 | -------------------------------------------------------------------------------- /ComparingObject.php: -------------------------------------------------------------------------------- 1 | id = "1"; 7 | $student1->name = "Eko"; 8 | $student1->value = 100; 9 | 10 | $student2 = new Student(); 11 | $student2->id = "1"; 12 | $student2->name = "Eko"; 13 | $student2->value = 100; 14 | 15 | var_dump($student1 == $student2); 16 | var_dump($student1 === $student2); 17 | var_dump($student1 === $student1); 18 | -------------------------------------------------------------------------------- /Constant.php: -------------------------------------------------------------------------------- 1 | adopt("Luna"); 9 | $cat->eat(new \Data\AnimalFood()); 10 | 11 | $dogShelter = new \Data\DogShelter(); 12 | $dog = $dogShelter->adopt("Doggy"); 13 | $dog->eat(new \Data\Food()); 14 | -------------------------------------------------------------------------------- /DateTime.php: -------------------------------------------------------------------------------- 1 | setDate(1990, 1, 20); 5 | $dateTime->setTime(10, 10, 10, 0); 6 | 7 | $dateTime->add(new DateInterval("P1Y")); 8 | 9 | $minusOneMonth = new DateInterval("P1M"); 10 | $minusOneMonth->invert = true; 11 | $dateTime->add($minusOneMonth); 12 | 13 | var_dump($dateTime); 14 | 15 | $now = new DateTime(); 16 | var_dump($now); 17 | $now->setTimezone(new DateTimeZone("America/Toronto")); 18 | var_dump($now); 19 | 20 | $string = $now->format("Y-m-d H:i:s"); 21 | echo "Waktu Saat Ini : $string" . PHP_EOL; 22 | 23 | $date = DateTime::createFromFormat("Y-m-d H:i:s", "2020-10-10 10:10:10", new DateTimeZone("Asia/Jakarta")); 24 | if ($date) { 25 | var_dump($date); 26 | } else { 27 | echo "Format Salah" . PHP_EOL; 28 | } -------------------------------------------------------------------------------- /DebugInfo.php: -------------------------------------------------------------------------------- 1 | id = "1"; 7 | $student1->name = "Eko"; 8 | $student1->value = 100; 9 | $student1->setSample("SAMPLE"); 10 | 11 | var_dump($student1); 12 | -------------------------------------------------------------------------------- /Destrcutor.php: -------------------------------------------------------------------------------- 1 | username = " "; 9 | $loginRequest->password = " "; 10 | 11 | try { 12 | validateLoginRequest($loginRequest); 13 | echo "VALID" . PHP_EOL; 14 | } catch (ValidationException | Exception $exception) { 15 | echo "Error : {$exception->getMessage()}" . PHP_EOL; 16 | 17 | var_dump($exception->getTrace()); 18 | 19 | echo $exception->getTraceAsString() . PHP_EOL; 20 | } finally { 21 | echo "ERROR ATAU ENGGAK, AKAN DIEKSEKUSI" . PHP_EOL; 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /Function.php: -------------------------------------------------------------------------------- 1 | name = "Eko"; 7 | $eko->sayHello("Budi"); 8 | 9 | $joko = new Person("Joko", "Cirebon"); 10 | $joko->name = "Joko"; 11 | $joko->sayHello(null); 12 | 13 | $eko->info(); 14 | $joko->info(); -------------------------------------------------------------------------------- /FunctionOverriding.php: -------------------------------------------------------------------------------- 1 | name = "Budi"; 7 | $manager->sayHello("Joko"); 8 | 9 | $vp = new VicePresident(); 10 | $vp->name = "Eko"; 11 | $vp->sayHello("Joko"); 12 | -------------------------------------------------------------------------------- /Generator.php: -------------------------------------------------------------------------------- 1 | setName("Handphone"); 7 | $category->setExpensive(true); 8 | 9 | $category->setName(" "); 10 | echo "Name : {$category->getName()}" . PHP_EOL; 11 | echo "Expensive : {$category->isExpensive()}" . PHP_EOL; 12 | -------------------------------------------------------------------------------- /Import.php: -------------------------------------------------------------------------------- 1 | name = "Budi"; 7 | $manager->sayHello("Joko"); 8 | 9 | $vp = new VicePresident(); 10 | $vp->name = "Eko"; 11 | $vp->sayHello("Joko"); 12 | -------------------------------------------------------------------------------- /Interface.php: -------------------------------------------------------------------------------- 1 | drive(); 8 | -------------------------------------------------------------------------------- /Invoke.php: -------------------------------------------------------------------------------- 1 | id = "1"; 7 | $student1->name = "Eko"; 8 | $student1->value = 100; 9 | 10 | $student1(1, "eko", true, "kurniawan"); 11 | -------------------------------------------------------------------------------- /Namespace.php: -------------------------------------------------------------------------------- 1 | id = "1"; 7 | $student1->name = "Eko"; 8 | $student1->value = 100; 9 | $student1->setSample("XXX"); 10 | 11 | var_dump($student1); 12 | 13 | $student2 = clone $student1; 14 | var_dump($student2); 15 | 16 | // cara manual clone 17 | //$student2 = new Student(); 18 | //$student2->id = $student1->id; 19 | //$student2->name = $student1->name; 20 | //$student2->value = $student1->value; 21 | 22 | // $student1 => clone $student2 => $student2->__clone() -------------------------------------------------------------------------------- /ObjectIteration.php: -------------------------------------------------------------------------------- 1 | $this->first, 14 | // "second" => $this->second, 15 | // "third" => $this->third, 16 | // "forth" => $this->forth, 17 | // ]; 18 | // 19 | // return new ArrayIterator($array); 20 | // } 21 | 22 | public function getIterator() 23 | { 24 | yield "first" => $this->first; 25 | yield "second" => $this->second; 26 | yield "third" => $this->third; 27 | yield "forth" => $this->forth; 28 | } 29 | } 30 | 31 | $data = new Data(); 32 | 33 | foreach ($data as $property => $value) { 34 | echo "$property : $value" . PHP_EOL; 35 | } 36 | -------------------------------------------------------------------------------- /Parent.php: -------------------------------------------------------------------------------- 1 | getCorner() . PHP_EOL; 9 | 10 | $rectangle = new Rectangle(); 11 | echo $rectangle->getCorner() . PHP_EOL; 12 | echo $rectangle->getParentCorner() . PHP_EOL; 13 | -------------------------------------------------------------------------------- /Polymorphism.php: -------------------------------------------------------------------------------- 1 | programmer = new Programmer("Eko"); 7 | var_dump($company); 8 | 9 | $company->programmer = new BackendProgrammer("Eko"); 10 | var_dump($company); 11 | 12 | $company->programmer = new FrontendProgrammer("Eko"); 13 | var_dump($company); 14 | 15 | sayHelloProgrammer(new Programmer("Eko")); 16 | sayHelloProgrammer(new BackendProgrammer("Eko")); 17 | sayHelloProgrammer(new FrontendProgrammer("Eko")); -------------------------------------------------------------------------------- /Properties.php: -------------------------------------------------------------------------------- 1 | name = "Eko"; 7 | $person->address = "Subang"; 8 | 9 | var_dump($person); 10 | 11 | echo "Name : $person->name" . PHP_EOL; 12 | echo "Address : $person->address" . PHP_EOL; 13 | echo "Country : $person->country" . PHP_EOL; 14 | 15 | $person2 = new Person("Budi", null); 16 | $person2->name = "Budi"; 17 | $person2->address = null; 18 | 19 | var_dump($person2); 20 | 21 | // error 22 | // $person2->name = []; 23 | -------------------------------------------------------------------------------- /PropertiesOverloading.php: -------------------------------------------------------------------------------- 1 | properties[$name]; 10 | } 11 | 12 | public function __set($name, $value) 13 | { 14 | $this->properties[$name] = $value; 15 | } 16 | 17 | public function __isset($name): bool 18 | { 19 | return isset($this->properties[$name]); 20 | } 21 | 22 | public function __unset($name) 23 | { 24 | unset($this->properties[$name]); 25 | } 26 | 27 | public function __call($name, $arguments) 28 | { 29 | $join = join(",", $arguments); 30 | echo "Call function $name with arguments $join". PHP_EOL; 31 | } 32 | 33 | public static function __callStatic($name, $arguments) 34 | { 35 | $join = join(",", $arguments); 36 | echo "Call static function $name with arguments $join". PHP_EOL; 37 | } 38 | 39 | } 40 | 41 | $zero = new Zero(); 42 | $zero->firstName = "Eko"; 43 | $zero->middleName = "Kurniawan"; 44 | $zero->lastName = "Khannedy"; 45 | 46 | echo "First Name : $zero->firstName" . PHP_EOL; 47 | echo "Middle Name : $zero->middleName" . PHP_EOL; 48 | echo "Last Name : $zero->lastName" . PHP_EOL; 49 | 50 | $zero->sayHello("Eko", "Khannedy"); 51 | Zero::sayHello("Eko", "Khannedy"); -------------------------------------------------------------------------------- /Reflection.php: -------------------------------------------------------------------------------- 1 | username = "eko"; 9 | $request->password = "rahasia"; 10 | 11 | // ValidationUtil::validate($request); 12 | 13 | ValidationUtil::validateReflection($request); 14 | 15 | class RegisterUserRequest 16 | { 17 | public ?string $name; 18 | public ?string $address; 19 | public ?string $email; 20 | } 21 | 22 | $register = new RegisterUserRequest(); 23 | $register->name = "Eko"; 24 | $register->address = "Subang"; 25 | $register->email = "eko@gmail.com"; 26 | 27 | ValidationUtil::validateReflection($register); 28 | 29 | ValidationUtil::validate($register); -------------------------------------------------------------------------------- /RegularExpression.php: -------------------------------------------------------------------------------- 1 | "Eko", 5 | "middleName" => "Kurniawan", 6 | "lastName" => "Khannedy" 7 | ]; 8 | 9 | $object = (object)$array; 10 | 11 | var_dump($object); 12 | 13 | echo "First Name $object->firstName" . PHP_EOL; 14 | echo "Middle Name $object->middleName" . PHP_EOL; 15 | echo "Last Name $object->lastName" . PHP_EOL; 16 | 17 | $arrayLagi = (array) $object; 18 | var_dump($arrayLagi); 19 | 20 | require_once "data/Person.php"; 21 | 22 | $person = new Person("Eko", "Subang"); 23 | var_dump($person); 24 | 25 | $arrayPerson = (array) $person; 26 | var_dump($arrayPerson); -------------------------------------------------------------------------------- /ToString.php: -------------------------------------------------------------------------------- 1 | id = "1"; 7 | $student1->name = "Eko"; 8 | $student1->value = 100; 9 | 10 | $string = (string) $student1; 11 | echo $string . PHP_EOL; 12 | 13 | // bisa seperti ini 14 | echo $student1 . PHP_EOL; -------------------------------------------------------------------------------- /Trait.php: -------------------------------------------------------------------------------- 1 | goodBye("Joko"); 9 | $person->hello("Budi"); 10 | 11 | $person->name = "Eko"; 12 | var_dump($person); 13 | 14 | $person->run(); -------------------------------------------------------------------------------- /TraitConflict.php: -------------------------------------------------------------------------------- 1 | doA(); 39 | $sample->doB(); -------------------------------------------------------------------------------- /Visibility.php: -------------------------------------------------------------------------------- 1 | getName() . PHP_EOL; 8 | echo $product->getPrice() . PHP_EOL; 9 | 10 | $dummy = new ProductDummy("Dummy", 1000); 11 | $dummy->info(); -------------------------------------------------------------------------------- /data/Animal.php: -------------------------------------------------------------------------------- 1 | name is running" . PHP_EOL; 21 | } 22 | 23 | public function eat(AnimalFood $animalFood): void 24 | { 25 | echo "Cat is eating" . PHP_EOL; 26 | } 27 | } 28 | 29 | class Dog extends Animal 30 | { 31 | public function run(): void 32 | { 33 | echo "Dog $this->name is running" . PHP_EOL; 34 | } 35 | 36 | public function eat(Food $animalFood): void 37 | { 38 | echo "Dog is eating" . PHP_EOL; 39 | } 40 | } -------------------------------------------------------------------------------- /data/AnimalShelter.php: -------------------------------------------------------------------------------- 1 | name = $name; 18 | return $cat; 19 | } 20 | } 21 | 22 | class DogShelter implements AnimalShelter 23 | { 24 | public function adopt(string $name): Dog 25 | { 26 | $dog = new Dog(); 27 | $dog->name = $name; 28 | return $dog; 29 | } 30 | } -------------------------------------------------------------------------------- /data/Car.php: -------------------------------------------------------------------------------- 1 | name; 11 | } 12 | 13 | public function setName(string $name): void 14 | { 15 | if(trim($name) != ""){ 16 | $this->name = $name; 17 | } 18 | } 19 | 20 | public function isExpensive(): bool 21 | { 22 | return $this->expensive; 23 | } 24 | 25 | public function setExpensive(bool $expensive): void 26 | { 27 | $this->expensive = $expensive; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /data/Conflict.php: -------------------------------------------------------------------------------- 1 | name = $name; 11 | $this->title = $title; 12 | } 13 | 14 | function sayHello(string $name): void 15 | { 16 | echo "Hi $name, my name is Manager $this->name" . PHP_EOL; 17 | } 18 | } 19 | 20 | class VicePresident extends Manager 21 | { 22 | 23 | public function __construct(string $name = "") 24 | { 25 | // tidak wajib, tapi direkomendasikan 26 | parent::__construct($name, "VP"); 27 | } 28 | 29 | function sayHello(string $name): void 30 | { 31 | echo "Hi $name, my name is VP $this->name" . PHP_EOL; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /data/Person.php: -------------------------------------------------------------------------------- 1 | name = $name; 14 | $this->address = $address; 15 | } 16 | 17 | function sayHello(?string $name) 18 | { 19 | if (is_null($name)) { 20 | echo "Hi, my name is $this->name" . PHP_EOL; 21 | } else { 22 | echo "Hi $name, my name is $this->name" . PHP_EOL; 23 | } 24 | } 25 | 26 | function info() 27 | { 28 | echo "Author : " . self::AUTHOR . PHP_EOL; 29 | } 30 | 31 | function __destruct() 32 | { 33 | echo "Object person $this->name is destroyed" . PHP_EOL; 34 | } 35 | } -------------------------------------------------------------------------------- /data/Product.php: -------------------------------------------------------------------------------- 1 | name = $name; 11 | $this->price = $price; 12 | } 13 | 14 | public function getName(): string 15 | { 16 | return $this->name; 17 | } 18 | 19 | public function getPrice(): int 20 | { 21 | return $this->price; 22 | } 23 | } 24 | 25 | class ProductDummy extends Product 26 | { 27 | 28 | public function info() 29 | { 30 | echo "Name $this->name" . PHP_EOL; 31 | echo "Price $this->price" . PHP_EOL; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /data/Programmer.php: -------------------------------------------------------------------------------- 1 | name = $name; 11 | } 12 | 13 | } 14 | 15 | class BackendProgrammer extends Programmer 16 | { 17 | } 18 | 19 | class FrontendProgrammer extends Programmer 20 | { 21 | } 22 | 23 | class Company 24 | { 25 | public Programmer $programmer; 26 | } 27 | 28 | 29 | function sayHelloProgrammer(Programmer $programmer) 30 | { 31 | if ($programmer instanceof BackendProgrammer) { 32 | echo "Hello Backend Programmer $programmer->name" . PHP_EOL; 33 | } else if ($programmer instanceof FrontendProgrammer) { 34 | echo "Hello Frontend Programmer $programmer->name" . PHP_EOL; 35 | } else if ($programmer instanceof Programmer) { 36 | echo "Hello Programmer $programmer->name" . PHP_EOL; 37 | } 38 | } -------------------------------------------------------------------------------- /data/SayGoodBye.php: -------------------------------------------------------------------------------- 1 | name is running" . PHP_EOL; 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /data/Shape.php: -------------------------------------------------------------------------------- 1 | sample = $sample; 13 | } 14 | 15 | public function __clone() 16 | { 17 | unset($this->sample); 18 | } 19 | 20 | public function __toString(): string 21 | { 22 | return "Student id:$this->id, name:$this->name, value:$this->value"; 23 | } 24 | 25 | public function __invoke(...$arguments): void 26 | { 27 | $join = join(",", $arguments); 28 | echo "Invoke student with arguments $join" . PHP_EOL; 29 | } 30 | 31 | public function __debugInfo() 32 | { 33 | return [ 34 | "id" => $this->id, 35 | "name" => $this->name, 36 | "value" => $this->value, 37 | "sample" => $this->sample, 38 | "author" => "Eko", 39 | "version" => "1.0.0" 40 | ]; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /exception/ValidationException.php: -------------------------------------------------------------------------------- 1 | username)) { 6 | throw new ValidationException("Username is null"); 7 | } else if (!isset($request->password)) { 8 | throw new ValidationException("Password is null"); 9 | } else if (trim($request->username) == "") { 10 | throw new Exception("Username is empty"); 11 | } else if (trim($request->password) == "") { 12 | throw new Exception("Password is empty"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /helper/ValidationUtil.php: -------------------------------------------------------------------------------- 1 | username)) { 8 | throw new ValidationException("username is not set"); 9 | } else if (!isset($request->password)) { 10 | throw new ValidationException("password is not set"); 11 | } else if (is_null($request->username)) { 12 | throw new ValidationException("username is null"); 13 | } else if (is_null($request->password)) { 14 | throw new ValidationException("password is null"); 15 | } 16 | } 17 | 18 | static function validateReflection($request) 19 | { 20 | $reflection = new ReflectionClass($request); 21 | $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC); 22 | foreach ($properties as $property) { 23 | if (!$property->isInitialized($request)) { 24 | throw new ValidationException("$property->name is not set"); 25 | } else if (is_null($property->getValue($request))) { 26 | throw new ValidationException("$property->name is null"); 27 | } 28 | } 29 | } 30 | } 31 | --------------------------------------------------------------------------------