0

hi all..
I just want to ask,is it correct,in a class that we have built,PHP will first execute the constructor function before the others??

to make it clear is this is example :

<?php
class database {
	
	public function __construct() {
		$this->open_connection();
        }

	
	public $connection;
			
	public function open_connection() {
	   $this->connection= mysql_connect("localhost","root","mypassword");
	if(!$this->connection) {
		die ("connetion failed: " . mysql_error());
          } 	
	
        public function close_connection() {
	  $connection=mysql_close($this->connection);
         }

}


$database=new database(); 
?>

is it because of the PHP will execute the constructor function first,then i cannot write this code ? :

public function __construct () {
    $this->$sonnection;
 }

please help..
Thank You :)

Edited by samsons17: n/a

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by samsons17
0

The constructor is always executed first of all functions.
About your code, $this->$connection is invalid. You may want to use $this->connection.
But note, your connection must be active before using it anywhere in the class.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.