Page 1 of 1

procedural to OO design

Posted: Sun Apr 05, 2009 7:16 pm
by itp
I am converting some old procedural code to use classes. It works, but I starting to have problems with global variables.
Also when I sit back and look at the structure I realize that it is not very coherent. Can someone suggest what I could do to re-factor this to make it more OO and coherent. Here is a simplified version of what I have.

Code: Select all

 
<?php 
// file1.php
class class1
{
    function f1()
    {
        include("file2.php");
    }
}
$newClass = new class1();
$var1 = 999;
$newClass->f1();
?>
 
 
<?php 
// file2.php
class class2
{
    function f2()
    {
        global $var1;       
        echo $var1 . "<br>";
    }
}
$newClass2 = new class2();
$var1 = 888;
$newClass2->f2();
?>
 
 

Re: procedural to OO design

Posted: Sun Apr 05, 2009 10:30 pm
by requinix
OO is not the way to go in this case. Why are you even trying?

Re: procedural to OO design

Posted: Mon Apr 06, 2009 3:15 am
by Christopher

Code: Select all

<?php 
// file1.php
class class1
{
    protected $var1;
    function __construct($var1) {
        $this->var1 = $var1;
    }
    function f1() {
        include_once("file2.php");
        $newClass2 = new class2($this->var1);
        $newClass2->f2();
    }
}
$newClass = new class1(999);
$newClass->f1();
?>
 
 
<?php 
// file2.php
class class2
{
    protected $var1;
    function __construct($var1) {
        $this->var1 = $var1;
    }
    function f2()
    {
        echo $this->var1 . "<br>";
    }
}

Re: procedural to OO design

Posted: Fri Apr 10, 2009 12:48 pm
by itp
Thank you arborint, you were spot on.

Now I have dug myself another hole.

I am writing an application that runs on both mysql and IBM db2 database. It was working OK until I tried to make my database access functions into class methods.

I am getting intermitant error "Parameter 1 must be a ressource but a bool was passed" on line "$result = i5_query($string);". I know that the $string is not the problem because it works once and then fails the second time. I suspect that the way I set up function fnConnectSQL may be the problem. Maybe a scope issue?

Here are the essential bits.

Code: Select all

 
<?php
 
include_once("_dbAccess_Class.php");            // Class for database access        
 
class ClassIndex extends ClassDbAccess
{
    /* ------------------------------------------------------ */
    function fnTestPW($prmUsername,$prmPassword)
    /* ------------------------------------------------------ */
    {       
            
            if ($GLOBALS["server"] == "i5")
            $conn = i5_pconnect(9.9.9.9,USER,PASSW) or die(i5_errormsg());
            else
            $conn = mysql_connect(9.9.9.9,USER,PASSW) or die("Could not connect."); 
 
            $string = "SELECT * FROM PWFILE where VLUSID = " . $q. $prmUsername . $q .  
                       " and VLPWRD = " . $q . $prmPassword . $q  ;
                        
            $result = $this->fnConnectSQL($string);
            $row = $this->fnFetchArraySQL($result);
            if ($row == TRUE)
            {
                // do something 
            }
    }
}   
 
 
// _dbAccess_Class.php
class ClassDbAccess
{     
    /* ------------------------------------------------------ */
    function fnConnectSQL($string)
    /* ------------------------------------------------------ */
    {   
        if ($GLOBALS["server"] == "i5")
        {
            $result = i5_query($string);    
            if(!$result)            
            {
                echo "Error code2: " . i5_errno($result) . "<br>\n";
                echo "Error message: " . i5_errormsg($result) . "<br>\n";
            }   
            return $result;         
        }
        
        elseif ($GLOBALS["server"] == "MySql") 
        {
            $result = mysql_query($string);
            if (!$result) 
            {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $string;
                die($message);
            }           
            return $result;         
        }
    }
?>