Skip to content

Instantly share code, notes, and snippets.

@mexitek
Created October 24, 2011 23:13
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 mexitek/1310695 to your computer and use it in GitHub Desktop.
Save mexitek/1310695 to your computer and use it in GitHub Desktop.
Super and subclass variable inheritence
<?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; }
}
?>
<?
# 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>';
?>
@mexitek
Copy link
Author

mexitek commented Oct 24, 2011

In this example I use login and pass, but in reality they represent any variables or object you would like to pass down to your sub classes.

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