Jump to content

Why Doesnt This Work?


glenelkins

Recommended Posts

Hi

 

Here is my index.php code:

 

<?php

// Setup
require_once ( 'lib/dbconfig.php' );
require_once ( 'lib/config.php' );
require_once ( 'lib/routes.php' );

// Class files
require_once ( 'classes/database.class.php' );
require_once ( 'classes/core.class.php' );

// Create core
$Core =& new Core();

// Create database and establish connection
$Core->db =& new Database ( $database );

// Begin by loading main controller
require_once ( 'controllers/core.php' );

?>

 

Then controllers/core.php

 

<?php

// We need to grab the controller class they want
// and include file
$controller = ( isset ( $_GET['c'] ) ) ? $_GET['c'] : '';
$function = ( isset ( $_GET['f'] ) ) ? $_GET['f'] : '';

// If we have an empty controller
// user default
if ( empty ( $controller ) ) $controller = DEFAULT_CONTROLLER;

// Check the controller exists
if ( file_exists ( APPLICATION_FOLDER . CONTROLLERS_FOLDER . $controller . '.php' ) ) {

require_once ( APPLICATION_FOLDER . CONTROLLERS_FOLDER . $controller . '.php' );
    
    // Create object
    $controller =& new $controller();
    
    // if there is an index function, run
    // only if $function is empty
    if ( !empty ( $function ) && method_exists ( $controller, "$function" ) ) {
        
        $controller->$function();
            
    } else {
        
    if ( method_exists ( $controller, 'index' ) ) {
    
    		$controller->index();
        
    	}

    }

    
}

?>   

 

And finally, application/controllers/home.php  ( as it loads the default without $_GET['c'] )

 

<?php

class Home extends Core {

function Home() {
    
    }
    
    function index() {
    
	$this->db->fquery( "SELECT * FROM `test`" );
        
    }
    
}

?>

 

I get this error:

Fatal error: Call to a member function fquery() on a non-object in C:\PHP\www\adverset cms final\application\controllers\home.php on line 11

 

So why cant Home class access the "db" object inside Core?? If i set a variable in the Core class, say var $test = 'Hello';  Then in Home class do echo $this->test;    that works fine!!

Link to comment
https://forums.phpfreaks.com/topic/156395-why-doesnt-this-work/
Share on other sites

i dont follow? The home class was setup after core and extends it, so shouldnt it grab all properties within core?? It manages to use a static variable so why no this when it has been set before the second class is loaded??

 

 

Link to comment
https://forums.phpfreaks.com/topic/156395-why-doesnt-this-work/#findComment-823418
Share on other sites

I have modified core.php to make it work, like this:

 

<?php

// We need to grab the controller class they want
// and include file
$controller = ( isset ( $_GET['c'] ) ) ? $_GET['c'] : '';
$function = ( isset ( $_GET['f'] ) ) ? $_GET['f'] : '';

// If we have an empty controller
// user default
if ( empty ( $controller ) ) $controller = DEFAULT_CONTROLLER;

// Check the controller exists
if ( file_exists ( APPLICATION_FOLDER . CONTROLLERS_FOLDER . $controller . '.php' ) ) {

require_once ( APPLICATION_FOLDER . CONTROLLERS_FOLDER . $controller . '.php' );
    
    // Create object
    $controller =& new $controller();
    
    // Create the database object
    $controller->db = $Core->db;
    
    // if there is an index function, run
    // only if $function is empty
    if ( !empty ( $function ) && method_exists ( $controller, "$function" ) ) {
        
        $controller->$function();
            
    } else {
        
    if ( method_exists ( $controller, 'index' ) ) {
    
    		$controller->index();
        
    	}

    }

    
}

?>   

 

So now the following is working fine:

 

<?php

class Home extends Core {

function Home() {
    
    }
    
    function index() {
    
	$result = $this->db->fquery( "SELECT * FROM `test`" );
        
        echo "{$result->frows()}";
        
    }
    
}

?>

Link to comment
https://forums.phpfreaks.com/topic/156395-why-doesnt-this-work/#findComment-823420
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.