views:

212

answers:

2

I've been dabbling in writing a PHP Class for a few weeks now and I like to think I've got a handle on he basics but I'm a little stumped.

As a simplified example of what I'm doing:

I have declared and instantiated a public variable ($myURL) in my class (someClass) and in a external file (config.php) to the class filled the variable with a URL (http://demo.com).

In a function (make_array()) within the same class, I use the declared variable ($myURL) in a while() loop to build an associative array of data which I use outside the class to build a listed output (output.php).

Once I've set the variable using a class function (set_myurl($url)) in the external file (config.php) I can access it quite easily outside of the class file by using echo to print the set URL to the page.

The problem comes when I'm looping through the array and I want to set the predefined variable ($myURL) to various sub-arrays within the associative array that I'm building with the make_array() function in my class file. The make_array() function takes a MSSQL $result and loops the rows creating the array inserting the URL if one does not exist and the second argument ($add_my_url) is set to true.

Within the function the defined variable becomes empty but remains set as I've tested it with the built in PHP functions empty(), isset() and is_null() in an effort to troubleshoot the problem.

I wonder if anyone has any knowledge or experience of this happening to them.

A brief idea of what I'm doing

<?php
class someClass {
var $myURL = '';

public function set_myurl($url){
$this->myURL = $url;
}

public function make_array($db_result_array, $add_my_url = false){
$new_array = array();
while($row = $db_result_array){         
// build array
$array = array(
'Amount' => $row['Amount'],
'Description' => $row['Description'],
'URL' => ($add_my_url ? (!$row['URL'] ? $this->myURL : $row['URL']) : $row['URL'])
);
// merge array  
$new_array[] = $array;          
}       

return $new_array;
}

}
?>

<?php
// config.php
$myClass = new someClass;
$myClass->set_myurl('http://demo.com');

// this works and displays: http://demo.com
echo $myClass->myURL;
?>

I know this example is really abstract but I didn't want to post the full 2,000 lines of PHP.

If you need more clarification, please do ask. I'm quite confident that I've narrowed the problem down to this function and this while() loop.

Thanks.

+2  A: 

I believe something is wrong with your ternary condition, try this:

class someClass
{
    public $myURL = '';

    public function set_myurl($url)
    {
        $this->myURL = $url;
    }

    public function make_array($db_result_array, $add_my_url = false)
    {
        $new_array = array();

        while ($row = $db_result_array)
        {
            // build array
            $array = array
            (
                'Amount' => $row['Amount'],
                'Description' => $row['Description'],
                //'URL' => ($add_my_url ? (!$row['URL'] ? $this->myURL : $row['URL']) : $row['URL'])
            );

            if ($add_my_url === true)
            {
                $array['URL'] = $row['URL'];

                if (empty($array['URL']))
                {
                    $array['URL'] = $this->myURL;
                }
            }

            // merge array  
            $new_array[] = $array;          
        }    

        return $new_array;
    }
}
Alix Axel
Cryo
Thanks for the first reply. I don't think the problem is with the Ternary Condition, although it's probably not the most optimised method to do what I wanted to do as @Cryo pointed out.The problem is that the $this->myURL variable seems to be empty within the function despite appearing to be correctly set.It's a strange problem because it seems so simple.
paperclip
@paperclip If you print out $this->myURL as the very first line of the make_array() method is it still blank? If so it doesn't look like the code you've provided contains your problem, myURL is getting unset somewhere else (maybe a call to set_myurl('') stashed somewhere?). I'd suggest continually simplifying your code path through your class to try and flush out the problem method.
Cryo
A: 

Maybe the problem is here while($row = $db_result_array){

You should try a for/foreach maybe?

ign