Click to See Complete Forum and Search --> : extending classes


ShrineDesigns
10-04-2004, 08:32 PM
The extended or derived class has all variables and functions of the base class (this is called 'inheritance' despite the fact that nobody died) and what you add in the extended definition.i have several classes that use one class as a base class that holds basic mysql functions, why is sql_alias->connection not availiable in other sub classes, and how could i resolve this issue?

base class:class sql_alias
{
var $connection;

function affected()
{
return @mysql_affected_rows($this->connection);
}
function connect()
{
$this->connection = @mysql_pconnect(SQL_SERVER, SQL_USERNAME, SQL_PASSWORD);
return @mysql_select_db(SQL_DATABASE, $this->connection);
}
function error()
{
return @mysql_error($this->connection);
}
function fetch($result, $type = SQL_NUM)
{
return @mysql_fetch_array($result, $type);
}
function found()
{
$result = @mysql_query("SELECT FOUND_ROWS()", $this->connection);
return @mysql_result($result, 0);
}
function free($result)
{
return @mysql_free_result($result);
}
function num_fields($result)
{
return @mysql_num_fields($result);
}
function num_rows($result)
{
return @mysql_num_rows($result);
}
function query($query)
{
return @mysql_query($query, $this->connection);
}
function result($result, $row = 0, $field = NULL)
{
return @mysql_result($result, $row, $field);
}
}
$sql = new sql_alias;
a sub class:class category extends sql_alias
{
var $categories;

function category()
{
$category = array();
$result = parent::query("SELECT * FROM `categories`");

while($row = parent::fetch($result))
{
$category[] = $row;
}
parent::free($result);
$this->categories = $category;
}
}
$category = new category;

Paul Jr
10-04-2004, 09:01 PM
Are you having any specific problem accessing the variable? I copied your code as-is and I was able to access $connection from the category class without any problem.

ShrineDesigns
10-04-2004, 09:11 PM
hmmm...

if i run both of those classes, and i print_r() $category->categories it echoes an empty array and if i remove the @ from mysql_query it returns an error saying the link resource is not a valid resource

perhaps i missed or didn't replace a dll or something when i upgraded php 4.3.8 to 4.3.9

thank you

Paul Jr
10-04-2004, 09:25 PM
Glad to be of service. :)

ShrineDesigns
10-04-2004, 10:37 PM
i re installed php4 and apache then installed php5, and tested it on my remote server and it is still not working

rrr...

for something so simple has become a major headache lol

Paul Jr
10-04-2004, 11:48 PM
lol, I know what you mean.
So the problem is still that you can’t connect to the MySQL database, right? You haven’t had this problem before, have you?

ShrineDesigns
10-05-2004, 01:02 AM
i think i figured out why it is not working as i want it to, i think when i extend sql_alias into category it is making a new instance of sql_alias so, $connection is not defined within the scope of category

Paul Jr
10-05-2004, 02:38 PM
Ah, I see now. Is there any way to get around that?

ShrineDesigns
10-05-2004, 05:09 PM
i am using a constructor in the sub-class that calls sql_alias->connect()

that seemed to solve this issue

many thanks paul

Paul Jr
10-05-2004, 05:43 PM
No problem. Glad you got it working. ;)