Skip to content

Instantly share code, notes, and snippets.

@timmillwood
Created September 24, 2012 12:50
Show Gist options
  • Save timmillwood/3775799 to your computer and use it in GitHub Desktop.
Save timmillwood/3775799 to your computer and use it in GitHub Desktop.
PHP for Developers - Exercise: Creating Classes
// index.php
<?php
function __autoload($classname) {
$filename = "./". $classname .".php";
include_once($filename);
}
$article = new Article('My Article', 'This is my article\'s body, it even escapes <strong>HTML</strong>');
var_dump($article);
echo 'Body:';
echo $article->getBody();
$series = new SeriesArticle('My series article', 'This is my series article\'s body, it even escapes <strong>HTML</strong>');
var_dump($series);
echo 'Body:';
echo $series->getBody();
// Article.php
<?php
class Article {
public function __construct($title, $body) {
$this->title = $title;
$this->body = $body;
}
public function getBody() {
$filtered_body = htmlspecialchars($this->body, ENT_QUOTES, 'UTF-8');
return $filtered_body;
}
}
// SeriesArticle.php
<?php
class SeriesArticle extends Article {
function __construct($title, $body) {
parent::__construct($title, $body);
$this->seriesid = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment