Problem accessing object properties

i’m new to PHP objects and i’ve having a problem. I have the following class declaration. I then instantiate an object of the class. but then trying to echo a property value has no effect. what am i doing wrong? thanks.


function fractionToDecimal ( $str ) {

	class decimalObject {
			var $matchStr;
			var $fraction;
			var $whole;
			var $numerator;
			var $denominator;
			var $decimal;
			var $text;
					
			function _construct ($ma, $fr, $wh, $nu, $den, $dec, $te ) {
					$this->matchStr = $ma;
					$this->fraction = $fr;
					$this->whole = $wh;
					$this->numerator = $nu;
					$this->denominator = $den;
					$this->decimal = $dec;
					$this->text = $te;
			}
	}

	$matchStr = "bleh";
	$fraction = "no";
	$whole = 1;
	$numerator = 2;
	$denominator = 3;
	$decimal = 4;
	$text = "wtf";

	$a = new decimalObject($matchStr, $fraction, $whole, $numerator, $denominator, $decimal, $text);
	return $a;

}

$str = "3/22 slices and hardy harhar";
$result = fractionToDecimal($str);
echo ($result->decimal);


should output “4”. but when i run the code nothing happens. thanks, G

add
error_reporting(E_ALL);
to the top of the script then move the class definition outside of your function. See where that gets you.

And maybe find some more recent tutorials. I’m assuming you have php 5.x. The var modifier is a php 4 thing. You would typically use public in php 5.

_construct() should be double underscore __construct()

You can also use the same class name for the contructor

…but you shouldn’t really as this will change in 5.4 IIRC.