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 :)

Member Avatar for nileshgr

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.

Member Avatar for rajarajan2017

If you dont want to execute the constructor then remove the constructor from the class.

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.