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.
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
DateTimeandDateIntervalclasses.
- PHP 8.1 or higher
- Composer
-
Clone this repository:
git clone https://github.com/your-username/php-learning-project.git cd php-learning-project -
Install Composer dependencies:
composer install
Run the index.php file to execute all demos:
php index.phpYou 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.
You can manually call specific functions (and pass arguments) using CLI options:
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,.
Call a function without arguments:
php index.php --call-func=callDemoClassCall 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=JohnDoeOutput:
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,7Output:
12
Note:
- The script automatically parses the arguments using
getoptandexplode.- If
--arg-funcis not provided, the function will be called without arguments.
├── 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 runningcomposer install).index.phpis the main entry point that loads and executes the demos.
This project is open source and available under the MIT License.