Reputation: 371
Hi I'm relatively new to OOP PHP and trying to get my head around a few concepts. I have two methods one public and one private.
The public function is parameter is being filled by a get value and then it uses the private method to query the database.
public function viewProject($id) {
if (!intval($id)) {
$this->projectError = 'The requested project must be a numeric value';
return false;
}
if (!$this->findProject($id)) {
$this->projectError = 'The specified project was not found.';
return false;
}
return true;
}
private function findProject($pid) {
$data = $this->_db->get("projects", array('id', "=", $pid));
return $data->results();
}
I want to be able to store the results from the findProject method in a var like
$this->projectName = //result here for name
However I'm not entirely sure how to access the results from the query in the public method.
Upvotes: 0
Views: 32
Reputation: 331
All poperties of a class, public, protected and private can be accessed in every method of that class. If you define projectName as a (private) property, it can be accessed in every other method.
Also, your query result is probably a multi-dimensional array, so you have to retrieve the projectName value yourself from the result.
class A
{
protected $projectName;
public function viewProject($id) {
if (!intval($id)) {
$this->projectError = 'The requested project must be a numeric value';
return false;
}
$results = $this->findProject($id);
if (!$results) {
$this->projectError = 'The specified project was not found.';
return false;
}
//Parse results
//assuming $this->_db->get() returns a multi-dimensional array
//assuming 'projectName' corresponds is the db column name
$this->projectName = $results[0]['projectName'];
return true;
}
private function findProject($pid) {
$data = $this->_db->get("projects", array('id', "=", $pid));
return $data->results();
}
}
Upvotes: 1
Reputation: 1787
Try
public function viewProject($id) {
if (!intval($id)) {
$this->projectError = 'The requested project must be a numeric value';
return false;
}
$this->$project = $this->findProject($id); //project has the value
if (!$project) {
$this->projectError = 'The specified project was not found.';
return false;
}
return true;
}
private function findProject($pid) {
$data = $this->_db->get("projects", array('id', "=", $pid));
return $data->results();
}
hope it helps :)
Upvotes: 0