Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created June 21, 2013 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhulse/5833826 to your computer and use it in GitHub Desktop.
Save mhulse/5833826 to your computer and use it in GitHub Desktop.
Just playing with PHP5 classes. Want to know: 1) Is this a good/bad way to use an abstract class? If bad, what's the optimal way to setup this code? 2) Is there any way to skip passing the variable $foo to the abstract class, yet allow the abstract class to have a constructor that does things?
<p>Hello!</p>
<hr>
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
include_once('class.child.php');
new Foo_Child();
?>
<hr>
<p>Goodbye.</p>
<?php
include_once('class.abstract.php');
class Foo_Child extends Foo_Abstact
{
public function __construct() {
$foo = 'this is foo';
parent::__construct($foo);
echo sprintf('<p>%s: %s</p>', __CLASS__, $foo);
}
}
<?php
include_once('class.parent.php');
abstract class Foo_Abstact extends Foo_Parent
{
public function __construct($foo) {
parent::__construct($foo);
echo sprintf('<p>%s: %s</p>', __CLASS__, $foo);
}
}
<?php
class Foo_Parent
{
public function __construct($foo = 'nope') {
echo sprintf('<p>%s: %s</p>', __CLASS__, $foo);
}
}

Hello!


Foo_Parent: this is foo

Foo_Abstact: this is foo

Foo_Child: this is foo


Goodbye.

@mhulse
Copy link
Author

mhulse commented Jun 21, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment