How do I give reference to functions from one class to another?
The following is my simplified code
<?php
$database = new db();
$file = new file();
$input = new input();
$output = new output();
$data = "SELECT * FROM table;";
$input->page($data);
class db {
public function queryExecute($var) {
$var = $this->queryEncode($var);
$var = $this->querySubmit($var);
return $var;
}
public function queryEncode($var) {
// Do somthing
return $var;
}
public function querySubmit($var) {
// Do somthing
return $var;
}
}
The issue is when I add this to the code:
class input {
public function page($data) {
// Do something
$pageQuery = db::queryExecute($data);
}
}
With this, there are two things I have to do. First, I have to hide the errors for the db::queryExecute($data);
code if the server is set to strict. And now for the second problem. I can't seem to use this line of code (which is the only way I have yet found possible for referencing other classes besides using Abstract) if the class that is being referenced is referencing yet another class but this time within it's own class.
For better explanation, the procedure is as follows:
- Grab the
$data
variable and send it to the$input->page()
function ($input->page($data)
). - Referencing the
db
class, the$input->page()
function sends the information onto the$database->queryExecute()
function by means of thedb::queryExecute()
format (db::queryExecute($data)
). - But because we are using the
::
, when$database->queryExecute()
references$database->queryEncode()
and$database->querySubmit()
using$this->
operator (开发者_如何学C$this->queryEncode()
and$this->querySubmit()
),$this->
currently belongs to$input->
and not$database->
.
So what's the solution... Reference the other class differently (instead of ::
)? Use a $_GLOBAL
variable when I define my classes? Use something other than $this->
? Configure all of the classes to use ABSTRACT
/EXTENDS
(or INTERFACE
)?
The following error outputted refers to $var = $this->queryEncode($var);
:
Fatal error: Call to undefined method input::querySubmit() in C:\[...]\global.php on line 12
Do not make static call to not static function. Pass $db instance
to page, or provide global access to the database (via global registry, singleton or other method). But best, pass the dependency - the database instance, to the method.
<?php
$database = new db();
$file = new file();
$input = new input($database);
$output = new output();
$data = "SELECT * FROM table;";
$input->page($data);
class db {
public function queryExecute($var) {
$var = $this->queryEncode($var);
$var = $this->querySubmit($var);
return $var;
}
public function queryEncode($var) {
// Do somthing
return $var;
}
public function querySubmit($var) {
// Do somthing
return $var;
}
}
class input {
protected $_database;
public function __construct($database) {
$this->_database = $database;
}
public function page($data) {
// Do something
$pageQuery = $this->_database->queryExecute($data);
}
}
You can only use the double-colon operator on static, constant, or overridden properties or methods of a class. See the documentation on this. Use ->
instead.
精彩评论