|
-
May 11th, 2006, 01:04 AM
#1
Thread Starter
New Member
[RESOLVED] Help with PHP5 constructor
Hi, I'm reading the Zend PHP tutorial (http://www.zend.com/php/beginners/php101-7.php) and I came across the constructor, public function _construct(). PHP5 is enabled on my server. However, I've discovered that I had to actually call the constructor. Isn't the constructor supposed to run on its own when we create a new class?
For example:
PHP Code:
include_once("db_settings.php");
class dbFunctions extends dbSettings {
// properties
public $db;
public $connector;
public $result;
// constructor
public function _construct() {
parent::_construct();
$this->connector = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
if(!$this->connector) {
die ("Could not connect to MySQL.");
}
$this->db = mysql_select_db($this->db_name,$this->connector);
if(!$this->db) {
die ("Could not select the database.");
}
print "success!";
}
... bunch of code...
and in install.php
PHP Code:
include_once("db_functions.php");
$connect = new dbFunctions();
$connect->_construct();
and that displays...
success!
However, if I remove $connect->_construct();, everything falls apart. My solution was to change public function _construct() to public function dbFunctions(). This is not what I want to do. How can I get _construct() to work on its own like it's supposed to? Thanks for any help.
Last edited by selfishy_me; May 11th, 2006 at 02:32 AM.
-
May 11th, 2006, 02:12 AM
#2
Re: Help with PHP5 constructor
Note that the constructor should be called __construct(), not _construct(). All magic object functions in PHP are prepended with two underscores.
P.s: If you put your code in [php]<?php ?>[/php] tags. It will come out nice and formatted.
-
May 11th, 2006, 02:41 AM
#3
Thread Starter
New Member
Re: Help with PHP5 constructor
Oooh I get it now. It works perfectly! Thanks!
BTW, can you explain the private marker to me? As far as I understand, if I mark my first class as final, then it means another class cannot extend the protected first class. If I mark the property variables as private, then these variables are only accessible within the class but not in another even if it's extended. The same thing results if I mark a method as private. Am I correct so far?
-
May 11th, 2006, 03:57 AM
#4
Re: Help with PHP5 constructor
Be careful throwing words like "protected" around. Protected is actually another level of scope, as with private and public 
If you make a class final then yes no other class can extend it.
Private members of a class are not accessible from outside that class. Protected members are accessible from any other class that is on the inheritance chain of that class - e.g. the base class, any class that extends it, etc.
So yes, you are correct.
-
May 11th, 2006, 04:18 AM
#5
Thread Starter
New Member
Re: Help with PHP5 constructor
Ehh. Now I'm getting confused lol.
So, if I have a bunch of classes...
class Alien
class Pizza
class JumpingJacks
I protect the methods and members inside Alien.
Pizza extends Alien.
JumpingJacks extends Pizza.
Pizza can access the protected methods and members inside Alien. JumpingJacks cannot access the protected methods and members inside Alien?
-
May 11th, 2006, 04:20 AM
#6
Re: Help with PHP5 constructor
No, it can, because Alien is part of JumpingJacks' inheritance chain.
Alien -> Pizza -> JumpingJacks
all can access each others protected members.
If on the other hand the members are private, none of the classes can access any of them except its own.
-
May 11th, 2006, 04:23 AM
#7
Re: Help with PHP5 constructor
 Originally Posted by selfishy_me
Ehh. Now I'm getting confused lol.
So, if I have a bunch of classes...
class Alien
class Pizza
class JumpingJacks
I protect the methods and members inside Alien.
Pizza extends Alien.
JumpingJacks extends Pizza.
Pizza can access the protected methods and members inside Alien. JumpingJacks cannot access the protected methods and members inside Alien?
http://www.vbforums.com/showpost.php...18&postcount=3
If you look here I have given quite a detailed explanation of how to use the visibility modifiers.
Last edited by visualAd; May 11th, 2006 at 04:28 AM.
-
May 11th, 2006, 06:09 AM
#8
Thread Starter
New Member
Re: Help with PHP5 constructor
Ok thanks. Last thing though, why didn't visualAd's tutorial come up when I searched all forums for the keyword "_construct()?"
-
May 11th, 2006, 06:21 AM
#9
Re: Help with PHP5 constructor
Probably becuase you should have searched for __construct()
-
May 11th, 2006, 02:27 PM
#10
Thread Starter
New Member
Re: Help with PHP5 constructor
But wouldn't it be more logical for the search function to search any text with _construct()? After all, like in my case, I didn't know I need the extra underscore and therefore, I couldn't find what I needed because of one underscore. 
Oh wells. Anyway, thanks visualAd and penagate for the help.
-
May 11th, 2006, 02:29 PM
#11
Re: [RESOLVED] Help with PHP5 constructor
We don't have fulltext searching. It only matches words.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|