In my opinion I'm requiring all necessary files but I keep getting the error that one file is not found. All files are in the same folder.
This is my code for zConfig.php:
<?php
class zConfig {
static $confArray;
public static function read($name) {
return self::$confArray[$name];
}
public static function write($name, $value) {
self::$confArray[$name] = $value;
}
}
?>
And this is my code for bd2.php:
require_once('zConfig.php');
/*
* Write settings to the config
*/
zConfig::write('hostname', 'XXXX');
zConfig::write('database', 'XXXX');
zConfig::write('username', 'XXXX');
zConfig::write('password', 'XXXX');
zConfig::write('drivers', array(PDO::ATTR_PERSISTENT => true));
Finally, this is my code for a function within Product.php:
public function pdo_updateTip($which, $tiptext)
{
require_once("bd2.php");
try {
$pdo = new PDO("mysql:host=".bd2::read('hostname').";dbname=".bd2::read('database')."", bd2::read('username'), bd2::read('password'), bd2::read('drivers'));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare("UPDATE tip SET tip_text = :tiptext WHERE product_id='0' AND item LIKE :which");
$stmt->execute(array(
':tiptext' => $tiptext,
':which' => $which
));
echo $stmt->rowCount(); // 1
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
return true;
}
The error I'm getting when I load Product.php is this:
Fatal error: Class 'bd2' not found in /home/xxxxx/Product.php on line 45
Line 45 is the line where I first refer to bd2
, here:
$pdo = new PDO("mysql:host=".bd2::read('hostname').";dbname=".bd2::read('database')."", bd2::read('username'), bd2::read('password'), bd2::read('drivers'));
I think that what you want to do is
$pdo = new PDO("mysql:host=".zConfig::read('hostname').";dbname=".zConfig::read('database')."", zConfig::read('username'), zConfig::read('password'), zConfig::read('drivers'));
bd2
is not a class and therefore it does not contain any static method read
. It seems that it is just a file that sets the configuration for your database by using the zConfig::write
method.
Let me show you the power of autoloading in PHP.
If you follow PSR-1, it will be quite simple.
Suppose you have a structure like this:
Project/
--conf/
--classes/
--index.php
So, you put this on conf
directory, with a name you prefer:
<?php
/**
* @author Henrique Barcelos <rick.hjpbarcelos@gmail.com>
* @copiright (c) 2013, Henrique Barcelos
*/
ini_set('display_errors', true);
error_reporting(E_ALL | E_STRICT);
setlocale(LC_ALL, array('es_ES', 'es_ES.iso-8859-1', 'es_ES.utf-8', 'spanish'));
ini_set('date.timezone', 'Europe/Madrid');
define('APP_ROOT', dirname(__DIR__) . DIRECTORY_SEPARATOR);
set_include_path(
implode(PATH_SEPARATOR,
array_unique(
array_merge(
array(
APP_ROOT . 'YOUR_CLASS_DIRECTORY',
),
explode(PATH_SEPARATOR, get_include_path())
)
)
)
);
{
function autoload($class) {
$file = sprintf("%s.php", str_replace('_', DIRECTORY_SEPARATOR, $class));
if (($classPath = stream_resolve_include_path($file)) != false) {
require $classPath;
}
}
spl_autoload_register('autoload', true);
}
In your index.php
you just do:
require 'conf/NAME_OF_THE_FILE';
And voilá, you will have all of you classes loaded. For example, your class db2
should be put right inside your class directory.
For the benefit of anyone who ends up here looking for an answer to the "class not found" problem. I require a PHP mailer script that I found somewhere on the net. My own script that does the requiring was written a long time ago and at the time it was working perfectly.
I am in the process of moving servers (with the new one using a later version of PHP). I too ran into this issue and searched high & low for a solution. In the end the problem turned out to be a very trivial one. It turned out that the PHP mailer script was using
<? ... ?>
and that was apparently enough to confuse my new server. I changed the starting tag to <?php and everything was hunkry dory again.