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.