Introduction to OOP
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 2 of 91.
Object-Oriented Programming (OOP) organizes code around objects that contain data (properties) and functions (methods).
Create a file called car.php with a class
<?php
class Car {
// empty class body
}Create another file called driver.php to use the class
<?php
require_once 'car.php';Create an object from the Car class using the new keyword
$myCar = new Car();Check the class of your object
echo get_class($myCar);Output:
CarThis confirms you've successfully created an object from your Car class. In OOP, a class is like a blueprint, and an object is what you build from that blueprint. The new keyword creates a new instance of the class.
Challenge
MediumIn this challenge, you'll use a Car class defined in car.php and create an object from it in driver.php.
You need to update driver.php:
- Create an object from the
Carclass using thenewkeyword and store it in$myCar
The driver file will print the class name to verify your implementation.
Cheat sheet
Object-Oriented Programming (OOP) organizes code around objects that contain data (properties) and functions (methods).
Define a class:
<?php
class Car {
// empty class body
}Include a class file in another file:
<?php
require_once 'car.php';Create an object from a class using the new keyword:
$myCar = new Car();Check the class of an object:
echo get_class($myCar); // Output: CarA class is like a blueprint, and an object is an instance created from that blueprint.
Try it yourself
<?php
require_once 'car.php';
// TODO: Create an object from the Car class
$myCar = ?;
// Print the class of $myCar
echo get_class($myCar) . "\n";
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe $this KeywordMethodsPropertiesConstructor (__construct)Destructor (__destruct)Recap - Simple Calculator4Inheritance
Basic InheritanceThe parent:: KeywordMethod OverridingThe final KeywordAbstract ClassesRecap - Employee Hierarchy7Encapsulation
Public, Protected, PrivateAccess Modifiers in DepthGetters and SettersInformation HidingConstructor Promotion (8.0)Recap - Student Records System2Namespaces & Autoloading
Introduction to NamespacesThe use KeywordPSR-4 Autoloading StandardComposer AutoloaderRecap - Organized Project5Interfaces & Contracts
Introduction to InterfacesImplementing InterfacesMultiple Interface ImplementInterface vs Abstract ClassType Hinting with InterfacesRecap - Shape Calculator8Magic Methods
Magic Methods Introduction__toString & __debugInfo__get, __set, __isset, __unset__call & __callStatic__clone & Object Cloning__serialize & __unserializeRecap - Custom Collection11Type System & Error Handling
Type DeclarationsNullable TypesUnion & Intersection TypesException ClassesCustom Exception HierarchyTry, Catch, FinallyRecap - Form Validator14Project: Library Management
Project OverviewBook and User Classes3Class Properties
Instance vs Static PropertiesConstants in ClassesStatic Methods & PropertiesPrivate & Protected PropertiesReadonly Properties (PHP 8.1)Recap - Bank Account Manager6Polymorphism
Method Overriding RevisitedPolymorphism via InterfacesType Hinting & Union TypesLate Static BindingRecap - Payment Processor9Traits
Introduction to TraitsUsing Multiple TraitsTrait Conflict ResolutionAbstract Methods in TraitsTraits vs Inheritance12Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern