note 68901 deleted from ref.mysqli by betz
| From: | betz@php.net | Date: | Tue, 15 Aug 2006 16:30:51 +0000 |
| Subject: | note 68901 deleted from ref.mysqli by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-115947@lists.php.net to get a copy of this message | ||
Note Submitter: info at azendal dot com
----
Mysqli (extending mysqli class the static method way)
I just started a new proyect and found that in fact parent::__construct() fails when extending
mysqli
Tuesday 1:00pm Just lunched and wanted to rock the world with my php code :P
Trial 1: Basic Extend mysqli testing phase
well extending rocks and all promissed by the manual is here (lol)
<?php
//pretty much what hans at lintoo dot dk did on hihs code but calling all is long
// Calling the monster
$db = new Controller('localhost', 'root', 'lalalala',
'test');
$db->executeSqlQuery('select * from clients');
echo '<table>'."\r\n";
echo
'<tr><td>ID</td><td>NOMBRE</td></tr>'."\r\n";
$db->iterateResult('<tr><td>%s</td><td>%s</td></tr>'."\r\n");
echo '</table>'."\r\n";
//
// well i think piece of cake let the mysqli contructor handles it
//
?>
*Tuesday 5:00pm just finished this, i rule i have time to watch tv a lil and eat some more
*Tuesday 7:00pm Well i think i must finish this or i wont get payed
*Trial 2: The real deal lets make a class that can be easily called Controller::executeSqlQuery
well that is the general idea because then some other classes will extend this one
<?php
class my_mysql1 extends controller {
protected function __contruct(){
parent::__construct('localhost','root','lalalala','test');
}
public static function execute_query (){
self::__construct();
$result = parent::query('select * from clients');
// iterate here
}
}
//calling the nice class
my_mysql1::execute_query();
?>
:( nothing happens even worst crashes my windows xp apache based server
i tracked that this is caused by calling self::__contruct
why would you ask??? ;)
well i just removed that line and it didnt crashed
BUT still not worked
*Tuesday 11:00pm $#"@! this is not working wtf is going on, logic says it should, well at least
my logic says so
*Tuesday 12:00pm i went to get a smoke, eat some more (thinking gets u hungry); Well google should
have the answer
and well try searching for a few minutes and going in all directions but i found something
http://youngcoders.com/archive/index.php?t-17896.html
they gave me a clue !!design patterns :|
and i tought ok so this could help me avoiding crash on constructor and still use static methods.
Wednesday 02:30am well seems a good solution let me think about it (but lets play a lil in the
meantime)
Wednesday 04:30am ok lets do it. after some small coding errors i got this:
<?php
class Controller extends mysqli {
private static $instance = NULL;
private static $result = NULL;
private static function getInstance(){
if (!self::$instance){
$c = __CLASS__;
self::$instance = new $c
('localhost','root','lalalala','test');
}
}
public static function executeSqlProcedure($procedure, $print, $format){
self::getInstance();
self::$result = self::$instance->query('call ' . $procedure);
if ($print == true){
self::printResultSet($format);
}
}
protected static function printResultSet ($format){
while ($row = self::$result->fetch_assoc()) {
vprintf ($format, $row);
}
}
}
?>
<?php
class Client extends Controller {
public static function listAll ($procedure = 'getClients()', $print = false, $format =
''){
parent::executeSqlProcedure($procedure, $print, $format);
}
// now the code gets to this
Client::listAll('getClients()', true, '%s%s');
?>
Output: 1fernando2liliana3daniel4yesus
hope t works for you and motivate u post ur solutions