Skip to content

Instantly share code, notes, and snippets.

@meltingice
Created September 25, 2009 14:20
Show Gist options
  • Save meltingice/193565 to your computer and use it in GitHub Desktop.
Save meltingice/193565 to your computer and use it in GitHub Desktop.
== About this Gist ==
I can't figure out wtf is going on here. Something strange with variable scope that I can't quite wrap my mind around.
If anyone knows the solution, please email me at [email protected]
This is a huge generalization of my code... some of the functions here aren't even a part of my codebase,
but they will still produce the same result and they are easier to understand because of their simplicity.
Also, I am using either PHP 5.3.0 or PHP 5.2.8 for this code.
== Where to Start ==
The user loads testfile.php in their web browser which includes config.php. You can probably follow it from there pretty easily.
PHP prints out the error I commented in testfile.php (except, you know, with the filename and line number instead of the class and function).
<?
include('osimo.php');
$osimo = new Osimo();
?>
<?
class OsimoDB{
function OsimoDB(){
// options stuff
}
// ...
// lots of other stuff
// ...
/*
* Not a real function in my class, but serves the same purpose
*/
public function doQuery($sql){
get('cache')->sqlquery($sql); //$osimo->cache is not defined/found
}
}
<?
class Osimo{
function Osimo(){
// ...
// set some options here
// ...
$this->init();
}
private function init(){
$this->loadIncludes();
$this->cache = new OsimoCache();
$this->db = new OsimoDB();
$this->loadConfig();
}
public static function loadIncludes(){
include_once('db.php');
include_once('cache.php');
// etc...
}
private function loadConfig(){
$this->db->doQuery('sql query'); //assume it knows to check the cache in this generalization
}
}
function get($class){
global $osimo;
if($class=='osimo' && isset($osimo)){
return $osimo;
}
if(is_object($osimo->$class)){
return $osimo->$class;
}
}
?>
<?
include('config.php');
// Fatal error: Call to a member function sqlquery() on a non-object in OsimoDB::doQuery()
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment