Page 1 of 1

Class __construct

Posted: Tue Jun 05, 2007 11:36 am
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]

Posted: Tue Jun 05, 2007 11:51 am
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.

Posted: Tue Jun 05, 2007 12:00 pm
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";

Posted: Tue Jun 05, 2007 12:31 pm
by Ollie Saunders

Code: Select all

... extends mySQLi
Classes are case sensitive that's why volka's is working.

Posted: Tue Jun 05, 2007 12:34 pm
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

Posted: Tue Jun 05, 2007 12:39 pm
by olog-hai
Thanks for your help

it's working.