Answer the question
In order to leave comments, you need to log in
How to call a method of one object from another object?
I recently started learning OOP in PHP, and there was a problem in applying knowledge in practice. Let's say I have a MySQL class for connecting to a database, and there is another class, let's call it Page, where a MySQL class method is called.
class MySQL {
public $Vars;
function Connect () {
//code
}
}
class Page {
public $Vars;
function Func () {
$MySQL->Connect();
//code
}
}
$MySQL = new MySQL;
$Page = new Page;
$Page->Func()
Answer the question
In order to leave comments, you need to log in
for example, create an object of the MySQL class in the constructor of the Page class and refer to it in all functions
class MySQL
{
public function connect()
{
// code...
}
}
class Page
{
protected $mysql = null;
public function __constuct()
{
$this->mysql = new MySQL;
}
public function view($id)
{
$this->mysql->where($id);
}
public function delete($id)
{
$this->mysql->delete($id);
}
}
$page = new Page;
$page->view($id);
$page->delete($id);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question