You are here: Re: Extending the mysqli class « PHP Programming Language « IT news, forums, messages
Re: Extending the mysqli class

Posted by Daz on 02/25/07 16:00

On Feb 25, 3:41 pm, "Daz" <cutenfu...@gmail.com> wrote:
> Hi everyone.
>
> I am trying to create an extension of the mysqli class within PHP, and
> I am finding it quite difficult. I am fairly new to PHP classes, and
> decided to give them a go. Here's what I have to far:
>
> <?php
> class sql_db extends mysqli
> {
> var $connection = false;
> function sql_db($username, $password, $database="",
> $server="localhost")
> {
> $this->connection = new mysqli($server, $username,
> $password, $database);
> if (mysqli_connect_errno()) {
> printf("Connect failed: %s\n",
> mysqli_connect_error());
> exit();
> }
> return = $this->connection;
> }
> }
> ?>
>
> All I am trying to achieve for now is for a simple error to be printed
> if the connection to the database fails. Rather than have error
> checking for each within each PHP file, I would just like to have it
> all done systematically. I would also like to add a few of my own
> methods, such as having the object pass back a PHP array, rather than
> a MySQL array. I know I can do this with a separate function, but
> really I am doing this for the learning experience more than anything.
>
> At the present time, this works:
>
> $db =& new sql_db("myUsername", "myPass", "someDB");
>
> But as soon as I try to query a table, like so:
>
> $res = $db->query("SELECT * FROM `some_table`;");
>
> I get:
>
> Warning: mysqli::query(): Couldn't fetch sql_db in - on line 21
>
> Evidently I am doing something wrong, and I would really appreciate
> any pointers. I suspect that I can't actually do what I want to do,
> although I can't see why not. In any case, I am sure it's possible,
> but I am going about it completely wrong.
>
> Thanks in advance.
>
> Daz.


OK, I've done some more thinking, and I think that what I am trying to
do, would involve overriding the constructor of the mysqli class,
which I guess wouldn't be wise, and might not even be possible.

Is there any way to override the contructor and find out what the
constructor code is? I guess that it wouldn't be a PHP class as such,
but more of a hybrid, but I am uncertain.

I can see that what I am doing is correct for adding extra functions,
but I would appreciate it if anyone can help me figure out the best
way to do some error checking when the connection is opened.

I would also appreciate any input on overriding the default methods,
i.e whether it's possible or not, or whether it's ill advised, or if I
can actually use the original constructor and carefully modify it.

I think the initial error checking might require an external function
which instantiates the class, and does the checking, then returns the
mysqli object if all went well, or returns false if it didn't.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация