Forum Moderators: coopster

Message Too Old, No Replies

creating and using an object

trying to write to a file in an object

         

tekomp

7:17 am on Sep 29, 2005 (gmt 0)

10+ Year Member



I'm trying to create an object from a class I built that will write to a file. I've been trying forever now, but I cannot get it to work at all. I think it may have something to do with the scope of the variables, but not sure.

Here's what I have so far:


<?php

class Banner
{
var $startX;
var $startY;
var $textColor;
var $textSize;
var $text;
var $dataFile;
//var $delim = "¦";

function Banner($x1, $y1, $tc, $ts, $t, $df)
{
$this -> startX = $x1;
$this -> startY = $y1;
$this -> textColor = $tc;
$this -> textSize = $ts;
$this -> text = $t;
$this -> dataFile = $df;
}

function createFile()
{
$fh = fopen($dataFile, "a");
if(!$fh)
{
die("unable to open $dataFile);
}
fwrite($fh, $startX.$startY.$textColor.$textSize.$text);
fclose($fh);
}
}

$object = new Banner(1, 2, 3, 4, 5, 6);

object.createFile();

?>

Any tips?

tekomp

7:37 am on Sep 29, 2005 (gmt 0)

10+ Year Member



Ok, I'm pretty sure it's a variable scope problem, but still can't figure it out.

I changed the code to just display what values are in the object and not try to write anything to a file, and the values are showing up fine. This is what I changed to:


class Banner
{
var $startX;
var $startY;
var $textColor;
var $textSize;
var $text;
var $dataFile;
//var $delim = "¦";

function Banner($x1, $y1, $tc, $ts, $t, $df)
{
$this -> startX = $x1;
$this -> startY = $y1;
$this -> textColor = $tc;
$this -> textSize = $ts;
$this -> text = $t;
$this -> dataFile = $df;
}
}

$object = new Banner(1, 2, 3, 4, 5, 6);

echo "<pre>";
print_r($object);
echo "</pre>";
?>

And this is what it outputs:

banner Object
(
[startX] => 1
[startY] => 2
[textColor] => 3
[textSize] => 4
[text] => 5
[dataFile] => 6
)

I just can't figure out how to get the methods in the object to do stuff with the properties in the class (which are storing properly). Any help is appreciated!

tekomp

8:22 am on Sep 29, 2005 (gmt 0)

10+ Year Member



Nevermind. This was screwing everything up for some reason:

if(!$fh)
{
die("unable to open $dataFile);
}

coopster

12:46 pm on Sep 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, there is a scope issue (more like a variable refernce issue though) as well as a method syntax issue. First, the createFile() method of your class is referencing a variable incorrectly. Use the $this-> syntax instead of trying to reference the variable name directly as it is indeed outside the scope of the method:
function createFile() 
{
$fh = fopen($this->dataFile, "a");
if(!$fh) {
die("unable to open $this->dataFile);
}
fwrite($fh, $startX.$startY.$textColor.$textSize.$text);
fclose($fh);
}

Next, have a look at how you are trying to invoke that method ... incorrect syntax here ...
$object = new Banner(1, 2, 3, 4, 5, 6); 
//object.createFile(); <-- this is not correct syntax
$object->createFile();

Also, I like to turn error_reporting() [php.net] on in my classes when developing and troubleshooting. Errors like these come to the surface quite quickly and make it easier during development.