Instantiating objects in php

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi everyone. I'm a .Net developer and brand new to php. I've read
through most of the php manual and in doing so realized there are a lot
of similarities between c# and php. Anyway I have a specific question
about my subject. I ran across an issue while trying to instantiate an
object in a class. This is what I did.

<?php

include 'StringBuilder.php';

class ThisClass($someVar)
{
	private $sb = new StringBuilder();

public function SomeFunction()
	{
		$sb.append('Text');
		$sb.append('Text');
		echo $sb->toString();
	}
}

?>

}

I had to fix this by placing the object instantiation within a function
like so:


<?php

include 'StringBuilder.php';

class ThisClass($someVar)
{
private $sb;

public function SomeFunction()
{
$sb = new StringBuilder();

$sb.append('Text');
$sb.append('Text');
echo $sb->toString();
}
}

?>

}

I suppose the best way to handle this is to instantiate the object in
the constructor but I was wondering if anyone had any idea why this is
necessary in the first place?

-Wilby Jackson



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux