View Single Post
Old 07-14-2009, 03:20 PM   #1 (permalink)
russellharrower
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default Learning OOP the hard way! Please help

Hi,
I am designing my own CMS which i hope to release to the public, however this is my first project I have done using OOP...

Here in Australia there is NO one Tafe or University course that teaches OOP, I am wondering if someone would be kind to help me find out why the following is not working

I have the following files
config.php
dbopen.php
dbclose.php
index.php
articals.php

Now to explain what these files do.
Config.php
- has all the same things that Joomla has inside its config files. Just names a bit different and something left out, but looks the same.

dbopen.php
has the following code
PHP Code:
<?php
require_once("configuration.php");
$vconfig = new VConfig();

    
//Create objects from DBObject class.
    
$myObj = new DBConnect("localhost","$vconfig->user","$vconfig->dbpass","$vconfig->db");
    
/*
        You need to pass the values to the arguments for the constructor from 
        superclass DBConnect. If not $dbData won't know how to connect to database.
    */
   // $dbData = new DBData("localhost","$vconfig->user","$vconfig->dbpass","$vconfig->db");
    
    /*
        You need to connect to MySQL before work on any databases.
    */
    
$myObj ->connectToMySQL();
    
    
/*
        - Because the class DBData defined only one method selectDB(); 
        - Whenever you want to work with a database, you need to connect to MySQL first.
        - In your situation, You didn't call method connectToMySQL() from superclass DBObject.
        --> That's why You cannot select any databases.(Thats why you've got the errors)
    */
    //$dbData ->selectDB();

    
class  DBConnect{
        
/////////////////////////////////////////////////
        // PROTECTED PROPERTIES  
        /////////////////////////////////////////////////
        
protected $hostname$username$password,$db_name$con ;
        
     
/**
       * Constructor
       * @param String $hostname,$username,$password,$db_name. 
       * All information we need to provide whenever connect to db.
       */
        
public function __construct($hostname,$username,$password,$db_name){

            
$this->hostname $hostname;
            
$this->username $username;
            
$this->password $password;
            
$this->db_name $db_name;

        }
        
     
/**
       * Connect to MySQL.
       * @return void
       */
        
public function connectToMySQL(){
            
$this->con mysql_connect($this->hostname,$this->username,$this->password);
            if(
$this->con){
                echo 
"<br>Connected sucessfully to MySQL .<br>";
            }
            else{
                die( 
"<br>Could not connect to MySQL" mysql_error() . ".<br />");
            }
        }
    }      
 
?>
The next file that runs is artical.php

PHP Code:
<?php
require_once("configuration.php");
$vconfig = new VConfig();

$dbData = new DBData("localhost","$vconfig->user","$vconfig->dbpass","$vconfig->db");


class 
DBData extends DBConnect{
        
/* This class doesn't define any constructors. Whenever you create a object from  
         * this class, The object will automaticaly refer to constructor of 
         * super class (DBObject class). cause DBData extends from DBConnect class 
         */ 
        
        
public function title(){
            
$result=$DBData->query('SELECT * FROM push_content');
            
$row=$result->fetch_assoc();
            
//echo user's password
            
print $row['title'];
           
        }
        
        
    }
    

?>
and the closedb.php has the following
PHP Code:
   <?php
   
     
/*     You don't need to create a class just for closing a connection. 
     *    This method you can build  in DBConnect or DBObject class. But anyhow this is your option
     *    Maybe you have some special reason to have this class.
     */   


    
class DBClose extends DBConnect{
        function 
closeConnection(){
           
mysql_close();
        }
    }
    
?>
and in the index.php it links to each one of these using an include script.

the output in the index.php
PHP Code:
<?Php
//use joomla db class
require_once("configuration.php");
$vconfig = new VConfig();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en-gb"
lang="en-gb" >
<head>
<title><?php print $vconfig->site_title?></title>
<meta name="description" content="<?php print $vconfig->SiteDesc?>" />
<meta name="keywords" content="<?php print $vconfig->SEOKeys?>" />

</head>
<body>
<?php
require_once("include/dbconnect/opendb.php");
require_once(
"system/articals/artical.php");


    
$dbData ->title();


</
body>
</
html>
<?
php
require_once("include/dbconnect/dbclose.php");
?>
the code $dbData ->title();
makes this error. Fatal error: Call to a member function query() on a non-object in /home/desvisa/public_html/beta/system/articals/artical.php on line 15

which means this code
PHP Code:
    $result=$DBData->query('SELECT * FROM push_content'); 
is wrong but i dont know why,
can someone please help

Last edited by codefreek : 07-14-2009 at 06:59 PM. Reason: PHP tags added - please read http://www.talkphp.com/lounge/4563-prettifying-pasted-code-talkphp.html
russellharrower is offline  
Reply With Quote