Odd __autoload problem

I can’t figure out what’s happening here. All of my php scripts begin with “include(‘common.php’);”, where I set common variables and constants. In turn, common.php has “include(‘functions.php’);” near the top. All of the functions in functions.php are accessible, including function __autoload(). However, when I attempt to autoload a class, it doesn’t work.

For testing purposes, here’s my __autoload function:

[php]function __autoload($class)
{
echo “bye”;
die;
} // end function __autoload()

[/php]

Now, if I run the following script, everything works as expected, and “bye” is echoed to the screen:

[php]<?PHP
require_once(‘includes/functions.php’);
$test=new clTest();
?>
[/php]

Likewise, if I change the script to what’s shown below (note the call to ‘common.php’ instead of ‘functions.php’, “bye” is echoed to the screen:

[php]<?PHP
require_once(“includes/common.php”);
__autoload(“foo”);
?>[/php]

However, when I try the following script, I get “Fatal error: Class ‘clTest’ not found”:

[php]<?PHP
require_once(“includes/common.php”);
$test=new clTest();
?>[/php]

Can anyone help me out here?

can you post your functions and common page, I am curious about this one and want a better understanding of what is going on

Sure thing. Here’s common.php:

[php]<?php
define(“NL”,"\n");
define(“TAB”,"\t");
session_start();
if(!isset($addrPrefix)) $addrPrefix="";
include(“functions.php”);

//The following lines set basic constants after determining whether we’re on the
//development server or the production server
if($_SERVER[“DOCUMENT_ROOT”]=="/opt/lampp/htdocs")
{
define(“SITEDIR”,“http://localhost/ebay/”);
define(“ROOTDIR”,"//opt/lampp/htdocs/ebay/");
define(“SMARTYDIR”,"//opt/lampp/htdocs/ebay/includes/smarty/");
}
else
{
define(“SITEDIR”,“http://www.mydomain.com/ebay/”);
define(“ROOTDIR”,"/home/mydomain/public_html/ebay/");
define(“SMARTYDIR”,"/home/mydomain/public_html/ebay/includes/smarty/");
}

require(SMARTYDIR.“Smarty.class.php”);

class objSiteSmarty extends Smarty
{
function objSiteSmarty()
{
// Class Constructor. These automatically get set with each new instance.
parent::__construct();

	$this->template_dir = ROOTDIR.'includes/templates/';
	$this->compile_dir = ROOTDIR.'includes/templates_c/';
	$this->config_dir = ROOTDIR.'includes/configs/';
	$this->cache_dir = ROOTDIR.'includes/cache/';
	$this->assign('app_name','peekabay');

} // end function objSiteSmarty

} // end class objSiteSmarty

$smarty= new objSiteSmarty();
$smarty->assign(‘SITEDIR’,SITEDIR);
$smarty->assign(‘ROOTDIR’,ROOTDIR);
?>[/php]

And here’s functions.php:

[php]<?php

function __autoload($class)
{
echo “bye”;
die;
} // end function __autoload()

/*This is the actual __autoload function.
function __autoload($class)
{
$file=ROOTDIR.“includes/classes/”.$class.".php";
if(file_exists($file))
{
require($file);
}
else
{
die (“cannot instantiate class”);
}
} // end function __autoload()
*/

function string_entities($str)
{
return htmlentities(html_entity_decode($str));
}

function inToFeet($inches)
{
if($inches<1) return 0;
$ft=floor($inches/12);
$in=$inches%12;
return $ft."’ “.$in.”"";
} // end function intofeet

// Returns the whole part of a real number appended with .95
function dotNineFive($real)
{
return floor($real)+.95;
}

function odd($num)
{
return(is_int($num/2));
}

function InQuotes($strTempString,$dbl=true)
{
if($dbl)
{
return “”".$strTempString.""";
}
else
{
return “’”.$strTempString."’";
}
}

function viewArray($temp)
{
if(!is_array($temp)) return array(“error”);
foreach($temp as $k=>$v)
{
if(is_array($v))
{
echo “Subarray “.$k.”:
”;
viewArray($v);
echo “------

”;
}
else
{
echo $k." = “.$v.”
";
}
}
echo “
”;
}

//Generic Query String Building Function
function makeQS()
{
$strQS="";
$thisArr = func_get_arg(0);
$table = func_get_arg(1);
$mode = func_get_arg(2);
if (func_num_args() > 3) // filter for select, update or delete sent
{
$filter = func_get_arg(3);
}

switch ($mode)
{
case “add”:
$strQS=“INSERT INTO “.$table.” (”;
$strTemp = " VALUES (";
foreach($thisArr as $k=>$v)
{
$strQS .= $k.",";
$strTemp .= inQuotes($v).",";
}
$strTemp = rtrim($strTemp);
$strTemp = rtrim($strTemp,",").")";
$strQS = trim($strQS);
$strQS = trim($strQS,",").")";
$strQS .= $strTemp;
break;
case “get”:
$strQS=“SELECT * FROM “.$table;
if (is_array($filter))
{
$strQS .= " WHERE “;
foreach($filter as $k=>$v)
{
if (!trim($v))
{
}
else
{
$strQS .=$k.”=”.inQuotes($v).” AND ";

           }
        }
        $strQS = trim($strQS," AND ");
        $strQS = trim ($strQS," WHERE ");
     }
     break;
	case "update":
		$id=$thisArr["id"];
		unset($thisArr["id"]);
	  $strQS = "UPDATE ".$table." SET ";
		foreach($thisArr as $k=>$v)
		{
			if (is_numeric ( $v))
			{
			$strQS .= $k."=".$v.", ";
			}
			else
			{
			$strQS .= $k."="."'".$v."'".", ";
			}
		}
		$strQS = trim($strQS);
		$strQS = trim($strQS,",")."";
		$strQS .= " where id=".$id;
		break;
		case "delete":
		$strQS= "DELETE FROM ".$table;
		
		if ($table == "categories") {
			$strQS .= " WHERE cat_id = ".$filter;
			$strQS .= " OR parent_id = ".$filter;
		}
		else if ($table == "option_labels") {
			$strQS .= " WHERE optlabel_id = ".$filter;
		}
		else {
			$strQS .= " WHERE opt_id = ".$filter;
		}
		
		break;
  }

return $strQS;
}

?>[/php]

It’s all pretty basic stuff, really. Hope you can find a head-smacker in here somewhere. Thanks.

I don’t see the class clTest anywhere, did you error occur after you changed _autoload funtion and omitted

$file=ROOTDIR.“includes/classes/”.$class.".php";

You need to do the following for each class you wish to autoload.

function __autoload($class)
{

if($class==“clTest”){
require_once PATH TO clTest.class.php;
}

} // end function __autoload()

autoload is a magic function which is invoked when an object is instantiated without a defined class. autoloads $class parameter
is dynamically populated with the undefined, invoked class name

for instance when you try to create an instance of clTest… autoload is called because clTest is not defined. You’ll have to have some sort of conditional inside the _autoload function to tell php what the heck to do!

I prefer switch statements over if’s and usually default to a controller of sorts.

Hope that helps!

whoops didn’t read the entire script! sorry. Just read the first autoload.

Thanks, you guys, for trying to help. While this conversation was going on, I continued to investigate and discovered the solution. I’m using Smarty templates, and Smarty invokes spl_autoload_register(), which overrides __autoload.

I renamed __autoload to simply autoload and, after instantiating Smarty in my common.php file, I used spl_autoload_register() to register my own autoload function. That did the trick.

Thanks again for the time and comments.

Sponsor our Newsletter | Privacy Policy | Terms of Service