Created
October 24, 2011 23:13
-
-
Save mexitek/1310695 to your computer and use it in GitHub Desktop.
Super and subclass variable inheritence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Our super class | |
class A { | |
protected static $login; | |
protected static $pass; | |
# public static setter | |
# Maybe, replaces constructor | |
public static function setLogin($l,$p){ | |
self::$login = $l; | |
self::$pass = $p; | |
} | |
} | |
# Subclass 1 | |
class A1 extends A{ | |
function getLogin(){ return parent::$login; } | |
function getPass(){ return parent::$pass; } | |
} | |
# Subclass 2 | |
class A2 extends A{ | |
function getLogin(){ return parent::$login; } | |
function getPass(){ return parent::$pass; } | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
# Include our classes | |
include('classes.php'); | |
# Set defaults | |
A::setLogin( 'admin','qwerty' ); | |
# Set the login | |
$api1 = new A1(); | |
$api2 = new A2(); | |
# Write output | |
echo 'Testing A1: '.$api1->getLogin().' - '.$api1->getPass().'<br>'; | |
echo 'Testing A2: '.$api2->getLogin().' - '.$api2->getPass().'<br>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this example I use
login
andpass
, but in reality they represent any variables or object you would like to pass down to your sub classes.