PDA

Click to See Complete Forum and Search --> : [RESOLVED] Help with PHP5 constructor


selfishy_me
May 11th, 2006, 01:04 AM
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:

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


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.

visualAd
May 11th, 2006, 02:12 AM
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 ?> tags. It will come out nice and formatted. :)

selfishy_me
May 11th, 2006, 02:41 AM
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?

penagate
May 11th, 2006, 03:57 AM
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.

selfishy_me
May 11th, 2006, 04:18 AM
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?

penagate
May 11th, 2006, 04:20 AM
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.

visualAd
May 11th, 2006, 04:23 AM
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?p=2310018&postcount=3

If you look here I have given quite a detailed explanation of how to use the visibility modifiers.

selfishy_me
May 11th, 2006, 06:09 AM
Ok thanks. Last thing though, why didn't visualAd's tutorial come up when I searched all forums for the keyword "_construct()?"

visualAd
May 11th, 2006, 06:21 AM
Probably becuase you should have searched for __construct() :lol: ;)

selfishy_me
May 11th, 2006, 02:27 PM
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. :ehh:

Oh wells. Anyway, thanks visualAd and penagate for the help.

penagate
May 11th, 2006, 02:29 PM
We don't have fulltext searching. It only matches words.