Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Created December 10, 2023 19:47
Show Gist options
  • Save adeel-raza/bab8b315597bfd2841c52662ee0794f4 to your computer and use it in GitHub Desktop.
Save adeel-raza/bab8b315597bfd2841c52662ee0794f4 to your computer and use it in GitHub Desktop.
OOP PHP example to practice inheritence, constructors
<!-- Psudeo Code:
1. Media content class with properties title, release date and a method
to display info about the title and release date
2. Movies should contain genre and display additionally
3. Tv shows should contain num of seasons and display additionally
-->
<?php
class Media_Content {
public $title;
public $release_date;
public function __construct( $title, $release_date ) {
$this->title = $title;
$this->release_date = $release_date;
}
public function show_info() {
echo "The title is {$this->title} , the release date is {$this->release_date}<br><br>";
}
}
class Movies extends Media_Content {
public $genre;
public function __construct( $title, $release_date,$genre) {
parent::__construct( $title, $release_date );
$this->genre = $genre;
}
public function show_info() {
parent::show_info();
echo "and the genre is{$this->genre}<br><br>";
}
}
class Tv_Shows extends Movies {
public $num_of_seasons;
public function __construct( $title, $release_date, $genre, $num_of_seasons ) {
parent::__construct( $genre, $title, $release_date );
$this->num_of_seasons = $num_of_seasons;
}
public function show_info() {
parent::show_info();
echo "and the season is {$this->num_of_seasons}.<br><br>";
}
}
$media_content = new Media_Content( 'PSL9', '9/9/23' );
echo '<h2>Media Content</h2>';
$media_content->show_info();
echo '<hr />';
$movies = new Movies( 'Horror', 'The killer', '3/Sep/23' );
echo '<h2>Movies</h2>';
$movies->show_info();
echo '<hr />';
$tv_shows = new Tv_Shows( 'Homeland,', '6/4/23', 'Serial ', '4' );
echo '<h2>TV shows</h2>';
$tv_shows->show_info();
echo '<hr />';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment