Recap - Organized Project
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 14 of 91.
Challenge
EasyLet's build a complete blog application with proper namespace organization, bringing together everything you've learned about namespaces, the use keyword, and PSR-4 autoloading.
You'll create a well-structured project with five files that demonstrate how real PHP applications organize their code:
src/Models/Author.php— Define anAuthorclass in theBlog\Modelsnamespace. It should have apublic $nameproperty, a constructor that accepts and sets the name, and agetBio()method that returns"Author: [name]".src/Models/Post.php— Define aPostclass in theBlog\Modelsnamespace. It should havepublic $titleandpublic $authorproperties (where author is anAuthorobject). The constructor accepts a title and anAuthorobject, setting both. Add agetSummary()method that returns"[title] by [author name]".src/Services/Publisher.php— Define aPublisherclass in theBlog\Servicesnamespace. It should have apublish(Post $post)method that accepts aPostobject and returns"Published: [post title]". You'll need to import thePostclass using theusekeyword since it's in a different namespace.vendor/autoload.php— Create a PSR-4 autoloader usingspl_autoload_registerthat maps theBlog\namespace prefix to thesrc/directory. This single autoloader handles loading all your classes automatically.index.php— Your entry point. Require the autoloader, then use theusekeyword to importBlog\Models\Author,Blog\Models\Post, andBlog\Services\Publisher. Create anAuthornamed"Jane Smith", aPostwith title"Getting Started with PHP"using that author, and aPublisher. Print the author's bio, the post's summary, and the result of publishing the post (each on its own line).
Notice how the Publisher service depends on the Post model — this demonstrates how classes in different namespaces can work together by importing what they need with use statements.
Try it yourself
<?php
// Require the autoloader
require_once 'vendor/autoload.php';
// TODO: Import the necessary classes using 'use' statements
// - Blog\Models\Author
// - Blog\Models\Post
// - Blog\Services\Publisher
// TODO: Create an Author named "Jane Smith"
// TODO: Create a Post with title "Getting Started with PHP" using that author
// TODO: Create a Publisher
// TODO: Print the author's bio
// TODO: Print the post's summary
// TODO: Print the result of publishing the post
?>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