Jump to content

[SOLVED] OO PHP problem


akiznin

Recommended Posts

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

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"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.