Jump to content

PHP Classes


blmg2009

Recommended Posts

Hi there, 

 

This might a newbie question but I need help understanding PHP classes which am currently learning. 

 

I have an index page. 

 

With this three included files. 

 

database.php

config.php

account.php 

 

on database.php, the class is declared using $connection new Database(...), on this page is also all the coding for this class.

 

In config.php is a declared class of $account new Account($user_id);

 

and on account.php is all the details for the account class. 

 

on the index.php is echo $account->sayHello; 

 

However, My page is throwing out an error because I'm trying to use $connection->query(..) in my account.php / Account class. 

 

I have tried to extend the Account class with Database but still have no luck. 

 

How can I make sure the I can use a class function from another page in my Account class? 

 

 

 

Thanks for reading

Link to comment
https://forums.phpfreaks.com/topic/291407-php-classes/
Share on other sites

  Quote

How can I make sure the I can use a class function from another page in my Account class?

You need to pass your $connection instance into your Account class via the __construct and store it on a property.

 

class Account
{
    protected $db;

    public function __construct(Database $db)
    {
        $this->db = $db;
    }
}
You can now use your Database object within your Account object by referencing $this->db.
Link to comment
https://forums.phpfreaks.com/topic/291407-php-classes/#findComment-1492592
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.