/    Sign up×
Community /Pin to ProfileBookmark

PHP – using extend in classes

Hi Guys,

I’ve always included a small php file with the connection and then written classes that make a query and handle the return data.

This time, I am trying a different approach.

I created a class for the database connection and am using EXTEND for subsequent classes.

For example, I have:

[code]
class dao{
function connect_db(){
//connector function
}

function query($sql){
$this->query = mysql_query($sql);
return $this->query;
}

}

class website extends dao{
function getUsers(){
//get user function
}
}
[/code]

My question is how do I run the dao->query function from within the getUsers function?

Would it be something like:

[code]
function getUsers(){
$result = parent::query(“select * from tbl_user”);
while($r = mysql_fetch_assoc($result)){
echo $r[“user_name”].'<br />’;
}
}
[/code]

or can I just call it as if it were in the same class:

[code]
function getUsers(){
$result =$this->query(“select * from tbl_user”);
while($r = mysql_fetch_assoc($result)){
echo $r[“user_name”].'<br />’;
}
}
[/code]

Still trying to get a hang of classes and such. Thanks for you patience and help.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 17.2009 — Hi Guys,

...or can I just call it as if it were in the same class...
[/QUOTE]


Yes (as long as you did not declare the method to be "private" in the parent class).
Copy linkTweet thisAlerts:
@NogDogJun 17.2009 — PS: Note that the MySQL[b]i[/b] and PDO database extensions are already object-oriented, so you could simply(?) use them instead of creating your own OO implementation of the old MySQL extension.
Copy linkTweet thisAlerts:
@saturnprodsauthorJun 18.2009 — ps: Note that the mysql[b]i[/b] and pdo database extensions are already object-oriented, so you could simply(?) use them instead of creating your own oo implementation of the old mysql extension.[/quote]

you mean i've been reinventing fire and the wheel???

:d

thanks a million. I don't know how i missed this....[turns red]
Copy linkTweet thisAlerts:
@NogDogJun 18.2009 — you mean i've been reinventing fire and the wheel???

:d

thanks a million. I don't know how i missed this....[turns red][/QUOTE]


Probably because the MySQLi extension came out with PHP5, but there is an awful lot of legacy PHP4- code out there using the older MySQL extension, and even new code that feels a need to be PHP4-compatible (even though it's been close to a year now since PHP4 has ceased being supported in any way).
Copy linkTweet thisAlerts:
@saturnprodsauthorJul 05.2009 — NogDog, this has worked great. Thanks for your help.

But is it possible to write classes that extend this mysqli class?
Copy linkTweet thisAlerts:
@NogDogJul 05.2009 — NogDog, this has worked great. Thanks for your help.

But is it possible to write classes that extend this mysqli class?[/QUOTE]


Yes, for instance:
[code=php]
class Foo extends mysqli
{
public function __construct($host, $user, $password, $db)
{
parent::__construct($host, $user, $password, $db);
}

public function bar()
{
echo "This is my new method added to the mysqli class";
}
}
[/code]

But it all depends on what you actually need to do: perhaps it makes more sense to pass a MySQLi object to another class, for instance.
[code=php]
class Foo
{
private $db;

public function __construct(MySQLi $db)
{
$this->db = $db;
}

public function bar($id)
{
$stmt = $this->db->prepare("SELECT * FROM table WHERE id = ?");
$stmt->bind_param('i', $id);
// and so forth...
}
}

$db = new MySQLi('localhost', 'user', 'password', 'database');
$foo = new Foo($db);
$foo->bar();
[/code]
×

Success!

Help @saturnprods spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...