akiznin Posted July 29, 2009 Share Posted July 29, 2009 I am trying to get the hang of OO PHP, so I tried to create a simple script but it does not work, it won't echo. <?php class Framework { // MySQL Configuration private $database['hostname'] = 'localhost'; private $database['username'] = 'root'; private $database['password'] = ''; private $database['database'] = 'php'; private $database['connected'] = FALSE; // MySQL Connect public function hfm_connect() { $connect = mysql_connect($database['hostname'], $database['username'], $database['password']); if ($connect) { return 'Connected!'; } else { return 'Unable to connect!'; } } } $test = new Framework(); echo $test->hfm_connect(); ?> Link to comment https://forums.phpfreaks.com/topic/167919-solved-oo-php-problem/ Share on other sites More sharing options...
play_ Posted July 29, 2009 Share Posted July 29, 2009 The script gives me this error: Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION on line 6 Link to comment https://forums.phpfreaks.com/topic/167919-solved-oo-php-problem/#findComment-885673 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 you should declare the array first, then in a constructor member function, set each element... and reference the variables or arrays with the prefix $this-> // MySQL Configuration private $database = array(); public function __construct() { $this->database['hostname'] = 'localhost'; $this->database['username'] = 'root'; $this->database['password'] = ''; $this->database['database'] = 'php'; $this->database['connected'] = FALSE; } the constructor will run as soon as the class is instantiated with "new Framework", and needs to be named the same as the class or "__construct" Link to comment https://forums.phpfreaks.com/topic/167919-solved-oo-php-problem/#findComment-885676 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 sorry, sorry... should be __construct not __constructor Link to comment https://forums.phpfreaks.com/topic/167919-solved-oo-php-problem/#findComment-885682 Share on other sites More sharing options...
akiznin Posted July 29, 2009 Author Share Posted July 29, 2009 thanks OO is a little weird but i love how it works, not only if i could get it to work I making my own mini framework Link to comment https://forums.phpfreaks.com/topic/167919-solved-oo-php-problem/#findComment-885687 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.