I am fairly new to PHP OOP, the problem that I am have is that I can’t wrap my head around the follow layout of my script:
- main class is set which sets up the page and extends a mysql class and creates the database connect through the __construct
- within main class i run a public function which includes() a file and accesses a function that is in that include file
- within the function that is in the included file i can’t seem to access the main class through neither the actual global variable or use $this->blah
does anyone have any pointers or direction. i tried googling it but couldn’t come across anything remotely close to what i was trying to do.
it is started with: – works
$gw = new GWCMS();
then inside of the _construct of GWCMS() which GWCMS extends mySQL – works
parent::__construct(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$this->build();
then it calls build() – works
public function build(){
...
$page['content'] = $this->plugins($page['content']);
...
$output = $this->output($theme,$page);
echo eval('?>' . $output);
}
which calls plugins() – we start having problems
public function plugins($content){
$x = 0;
if ($handle = opendir(STOCKPLUGINPATH)) {
while (false !== ($entry = readdir($handle))) {
if(is_dir(STOCKPLUGINPATH . $entry . '/') && $entry != '.' && $entry != '..'){
if(file_exists(STOCKPLUGINPATH . $entry . '/inc.php')){
include(STOCKPLUGINPATH . $entry . '/inc.php');
$content = do_shortcode($content);
}
}
}
closedir($handle);
}
return $content;
}
the previous code includes inc.php which lists the files to be include:
include(STOCKPLUGINPATH . 'Test/test.php');
test.php includes the list of functions. the do_shortcode above accesses the functions without a problem and does the work however i need the following function which is in the test.php to access the $gw->fetchAssoc(); which fetchAssoc is in the parent of gwcms
function justtesting2($attr){
$config = $gw->fetchAssoc("Select * from gw_config");
foreach($config as $c){
echo $c['value'];
}
}
when i run the script i get
Fatal error: Call to a member function fetchAssoc() on a non-object in /home/globalwe/public_html/inhouse/GWCMS/gw-includes/plugins/Test/test.php on line 9
Writing OOP code means restructuring to avoid that mess of files and functions dropped into what ever files and god knows what not.
Try to rely on writing a class that models the behavior you want to achieve. The class should contain property values that carry data for you and methods that help the class behave like the thing you’re modeling it to.
To answer your question: