Results 1 to 11 of 11

Thread: [RESOLVED] Help with PHP5 constructor

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    14

    Resolved [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.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    14

    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?

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    14

    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?

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Help with PHP5 constructor

    Quote 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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    14

    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()?"

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Help with PHP5 constructor

    Probably becuase you should have searched for __construct()
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    14

    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.

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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
  •  



Click Here to Expand Forum to Full Width