Skip to content

tirtahakimpambudhi/learn-php-oop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Learning Project

Welcome to my PHP Learning Project!
This repository contains various demos showcasing PHP's Object-Oriented Programming (OOP) features, including namespaces, polymorphism, traits, iterators, cloning, magic methods, covariance, contravariance, and date handling.

Table of Contents

Features

This project demonstrates:

  • Basic OOP: Classes, objects, properties, and methods.
  • Inheritance: Parent and child class relationships.
  • Namespaces: Organizing code with namespaces.
  • Polymorphism: Using classes, abstract classes, and interfaces.
  • Traits: Reusing methods across multiple classes.
  • Anonymous Classes: Defining and using classes on the fly.
  • Cloning Objects: Implementing custom clone logic.
  • Magic Methods: Using __invoke, __toString, __get, __set, and more.
  • Covariance and Contravariance: Advanced typing examples.
  • Iterators: Manual, aggregate, and generator-based iteration.
  • Date and Time: Working with DateTime and DateInterval classes.

Requirements

  • PHP 8.1 or higher
  • Composer

Installation

  1. Clone this repository:

    git clone https://github.com/your-username/php-learning-project.git
    cd php-learning-project
  2. Install Composer dependencies:

    composer install

Usage

Run the index.php file to execute all demos:

php index.php

You can also call specific demo functions individually by modifying index.php according to what you want to test, such as:

callDemoClass();
callDemoInheritance();
callDemoNamespace();
callDemoPolymorphism();
callDemoTrait();
callDemoStdClass();
callDemoIterator();
callDemoClone();
callDemoMagicKeyword();
callDemoCovariance();
callDemoContravariance();
callDemoDate();

Each function demonstrates a different PHP feature.

Manual Testing with Arguments

You can manually call specific functions (and pass arguments) using CLI options:

Syntax:

php index.php --call-func=functionName --arg-func=arg1,arg2,...
  • --call-func: (Required) The name of the function you want to call.
  • --arg-func: (Optional) Arguments to pass to the function, separated by commas ,.

Examples:

Call a function without arguments:

php index.php --call-func=callDemoClass

Call a function with arguments:

Assuming you have a function like:

function greet($name) {
    echo "Hello, $name!";
}

You can call it like:

php index.php --call-func=greet --arg-func=JohnDoe

Output:

Hello, JohnDoe!

Call a function with multiple arguments:

Assuming you have:

function add($a, $b) {
    echo $a + $b;
}

You can run:

php index.php --call-func=add --arg-func=5,7

Output:

12

Note:

  • The script automatically parses the arguments using getopt and explode.
  • If --arg-func is not provided, the function will be called without arguments.

Project Structure

├── src/
│   └── OOP/
│       ├── Namespaces/
│       ├── Polymorphism/
│       ├── Traits/
│       ├── Clone/
│       ├── MagicKeyword/
│       ├── Covariance/
│       ├── Contravariance/
│       ├── Iterator/
│       └── Validation/
├── vendor/
├── composer.json
└── index.php
  • src/ contains all the OOP examples categorized by topic.
  • vendor/ contains Composer dependencies (after running composer install).
  • index.php is the main entry point that loads and executes the demos.

License

This project is open source and available under the MIT License.

About

Learn PHP Object Oriented Programming

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages