Solution 1
Replace class account_info { with class account_info extends connection {
Replace
$con = new connection(); $info = new account_info();
with
$info = new account_info();
and it should work.
Decision 2 (proposed)
I highly recommend you solve your problem with dependency injection in this case. Just replace the class of your account:
class account_info { private $con; public function __construct(connection $con) { $this->con = $con->con; } public function getAccountInfo(){ $acc_info = $this->con->prepare("SELECT * FROM account_info"); $acc_info->execute(); $results = $acc_info->fetchAll(PDO::FETCH_OBJ); foreach ($results as $key) { $results->owner_firstname; } } }
and use it in index.php as follows:
include_once 'classes/connection.class.php'; include_once 'classes/accountinfo.class.php'; $con = new connection(); $info = new account_info($con); $info->getAccountInfo();
Explanation
As a general correct rule: always specify the scope keyword for functions (public, protected or private).
The first solution is called inheritance, and basically we extended the account class with the connection class to inherit all the methods and properties from the connection class and easily use them. In this case, you should keep track of naming conflicts. I suggest you take a look at class inheritance in the PHP manual.
The second solution is called dependency injection, and it is a wildly encouraged design pattern that forces your classes to accept other classes in their constructor to explicitly define the class dependency tree (in this case, the account is connection-dependent and we cannot execute the account without connection).
Another, out of thousands of possible solutions, will be the one published below, which is a design pattern called Singleton. However, this patter has been recently rated as an anti-pattern and should not be used.
source share