Class __construct

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
olog-hai
Forum Commoner
Posts: 34
Joined: Thu May 31, 2007 8:47 am
Location: Québec city, QC, Canada

Class __construct

Post by olog-hai »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I have a derived class that extend mysqli, and I'm trying to initialize mysqli in my derived construct.
no succes.

i've to instanciate the mysqli object to get the connection !!, WHY ?

NOT working.

Code: Select all

class mySQL extends mySQLi
  function __construct( $db ){
    parent::__construct( "127.0.0.1", "login", "pwd", $db);
  }
end class

$obj = new mySQL();
WORKING

Code: Select all

class mySQL extends mySQLi
  public $_mySQLi;
  function __construct( $db ){
    $this->_mySQLi= new mysqli("127.0.0.1", "login", "pwd", $db);
  } 
end class

$obj = new mySQL();
thanks to help me.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by olog-hai on Tue Jun 05, 2007 11:51 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Using the class name as the constructor is the PHP4 style. PHP5 added __construct(). I believe that either now work in PHP5. Probably mysqli only supports the PHP4 style.
(#10850)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

olog-hai wrote:$obj = new mySQL();
missing parameter $db

working fine for me:

Code: Select all

<?php
class MyMySQL extends mysqli {
  function __construct( $db ){
    parent::__construct( "127.0.0.1", "localuser", "localpass", $db);
    if ( 0!==($errno=mysqli_connect_errno()) ) {
    	throw new Exception('...yadda yadda error message...');
    }
  }
}

echo "--\n";
$obj = new MyMySQL('test');
echo "--\n";
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

... extends mySQLi
Classes are case sensitive that's why volka's is working.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I don't think so ;)

Code: Select all

<?php
class abc {
}

class AbC {
}
Fatal error: Cannot redeclare class AbC in C:\Dokumente und Einstellungen\Volker\Desktop\test.php on line 5
User avatar
olog-hai
Forum Commoner
Posts: 34
Joined: Thu May 31, 2007 8:47 am
Location: Québec city, QC, Canada

Post by olog-hai »

Thanks for your help

it's working.
Post Reply