...

View Full Version : Help with class and variables



funnymoney
08-14-2007, 04:39 PM
Hy guys

I'm php newbie, looking for some guidance with php oop. I'm trying to get some variables into class but getting this error



Parse error: parse error, unexpected T_VARIABLE in D:\www\Apache2\htdocs\test.php on line 7

This is the full code



<?php
$variable = "my variable";

class phpClass {
public $main = $variable;

public function main() {

echo $this->main;

}

}

$mainis = new phpClass;

$mainis->main();

?>


Thanks for your help

firepages
08-14-2007, 05:38 PM
$variable is not within the scope of the class, e.g. it does not know it exists, you would normally set a default value

public $main='';

and populate $main via a setter or in the constructor

public function main($var){
$this->main=$var;
...etc

funnymoney
08-14-2007, 07:50 PM
So that is not how it's done.

Thanks for explaining the situation @oop newbie



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum