943,186 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 498
  • PHP RSS
Jun 20th, 2010
0

need helps about php constructor

Expand Post »
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 Syntax (Toggle Plain Text)
  1. <?php
  2. class database {
  3.  
  4. public function __construct() {
  5. $this->open_connection();
  6. }
  7.  
  8.  
  9. public $connection;
  10.  
  11. public function open_connection() {
  12. $this->connection= mysql_connect("localhost","root","mypassword");
  13. if(!$this->connection) {
  14. die ("connetion failed: " . mysql_error());
  15. }
  16.  
  17. public function close_connection() {
  18. $connection=mysql_close($this->connection);
  19. }
  20.  
  21. }
  22.  
  23.  
  24. $database=new database();
  25. ?>

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

PHP Syntax (Toggle Plain Text)
  1.  
  2. public function __construct () {
  3. $this->$sonnection;
  4. }


please help..
Thank You
Last edited by samsons17; Jun 20th, 2010 at 11:37 am.
Similar Threads
Reputation Points: 6
Solved Threads: 1
Posting Whiz in Training
samsons17 is offline Offline
225 posts
since Oct 2009
Jun 20th, 2010
0
Re: need helps about php constructor
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.
Reputation Points: 17
Solved Threads: 23
Junior Poster
nileshgr is offline Offline
162 posts
since Aug 2009
Jun 21st, 2010
-1
Re: need helps about php constructor
If you dont want to execute the constructor then remove the constructor from the class.
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008
Jun 22nd, 2010
0
Re: need helps about php constructor
Thanks
Reputation Points: 6
Solved Threads: 1
Posting Whiz in Training
samsons17 is offline Offline
225 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: protect post from other domain
Next Thread in PHP Forum Timeline: Session Value is missing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC