Hi All,
BORING INTRODUCTION
It's been many years since I was here-back when I last posted on phpfreaks I was but in my early teens and learning procedural php. I have since been through college, uni, and a decent web programming job where I have broadened my knowledge significantly to the point of doing everything in OOPHP . I know enough to get by but with some personal projects I am now working on I require a greater understanding of OOPHP (including calling things what they are e.g. object, method, property, etc (I always got mixed up with these). Apologies if what is below is a moment...but sometimes I don't quite understand what is written on these sites.
THE PROBLEM
I have my own simple custom DBAL. It works. I am now creating a class that needs the DBAL. The DBAL class file and class name are saved in a separate config file as constants. Something I have never been sure how to do (if possible) is if you can do the following:
The class that needs DBAL access (separate file):
public function __construct() {
require('cfg.php');
require(DBAL_CLASS);
$this->dbal = new DBAL_CLASS_NAME;
}
cfg.php
<?php
//Simpler directory desperator
define('DS', DIRECTORY_SEPARATOR);
//DBAL
define('DBAL_CLASS', $_SERVER['DOCUMENT_ROOT'] . DS . 'db' . DS . 'index.php');
//Name of DBAL
define('DBAL_CLASS_NAME', 'db');
A few things:
The file name and directory for the DBAL is correct. I have tested echo'ing the constants in the class requiring the DBAL and they are point to the correct place. The only issue with this is that the path for the directory starts off using / and then DS goes to using \. Would this be an issue?
I am aware that using constants in this way does not work. I have since edited this to assign the constants to variables as a temporary solution to that part, but I am still having problems. As I have mentioned above I am not from an OOP background and am doing my best to get to grips with it as I can see it's benefits over procedural and enjoy programming like this.
LONG STORY SHORT
I can't instantiate the class in the construct as I am trying there-well it won't work anyway. I am doing the exact same thing with another class in another file only a few lines later which works perfectly (i.e. I call methods from this other class in further methods in the current class). What have I done wrong? What could I do better? What is laughable? What have I done right? Any pro's and/or cons about the above would be greatly appreciated
As I said above-a mixture of simple and not so simple stuff (or so it seems to me ).
Thanks in advance,
Pudgemeister