oop matter
Moderator: General Moderators
oop matter
I wonder if there is nayone there, could help me with this issue, I have class called pages that suppose to query the abstract data layer and get the result as an array and then the class transform this result into json.
I created a testing class to instantiate this class and print the result. However I managed to print json form ($json_res;) from within the class but not from the testing.php, I don't understand this behave?!!
----------------------------------------------------------------------------
<?php
include '/Database.php';
class page{
private $page_id; // page_id
private $title;
private $discription;
public $res; // the result Array from DAL
public $json_res; // JSON transform from $res
// Constractors
function __construct($page_id)
{
//setters
$this->setPageId($page_id);
//getters
//$this->getPage($page_id);
}
// Setter Functions
public function setPageId($Inpage_id)
{
$this->setPageId = $Inpage_id;
}
// Getter Functions
public function getPage($page_id)
{
$db = new crud();
$db->connect();
$db->select('pages', '*' , 'page_id ='.$page_id);
$res = $db->getResult();
$json_res = JSON_encode($res);
echo $json_res; // this works fine?!!!
return $json_res;
}
}//End of page class
?>
--------------------------------------------------------------------------------
<?php
function __autoload($class) {
require_once("classes/$class.class.php");
}
$page_id=$_GET["q"]; // Create object of class page and assign the query that's been passed by Ajax call
$p = new page($page_id); // creating an object
echo $p->json_res; ///////this doesn't work at all ?!!!!!
?>
---------------------------------------------------------------------------------
I created a testing class to instantiate this class and print the result. However I managed to print json form ($json_res;) from within the class but not from the testing.php, I don't understand this behave?!!
----------------------------------------------------------------------------
<?php
include '/Database.php';
class page{
private $page_id; // page_id
private $title;
private $discription;
public $res; // the result Array from DAL
public $json_res; // JSON transform from $res
// Constractors
function __construct($page_id)
{
//setters
$this->setPageId($page_id);
//getters
//$this->getPage($page_id);
}
// Setter Functions
public function setPageId($Inpage_id)
{
$this->setPageId = $Inpage_id;
}
// Getter Functions
public function getPage($page_id)
{
$db = new crud();
$db->connect();
$db->select('pages', '*' , 'page_id ='.$page_id);
$res = $db->getResult();
$json_res = JSON_encode($res);
echo $json_res; // this works fine?!!!
return $json_res;
}
}//End of page class
?>
--------------------------------------------------------------------------------
<?php
function __autoload($class) {
require_once("classes/$class.class.php");
}
$page_id=$_GET["q"]; // Create object of class page and assign the query that's been passed by Ajax call
$p = new page($page_id); // creating an object
echo $p->json_res; ///////this doesn't work at all ?!!!!!
?>
---------------------------------------------------------------------------------
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: oop matter
I think its because this line is commented out:
in your example, nothing is calling getPage so $json_res is never filled
Code: Select all
//$this->getPage($page_id);Re: oop matter
$json_res only exists within your public function getPage($page_id) function.
So either you initiate the var in the constructor or:
echo $p->getPage($page_id);
So either you initiate the var in the constructor or:
echo $p->getPage($page_id);
Re: oop matter
I don't really think this the problem 'cos getPage() function should be called automatically by the constructor when the object is created.iankent wrote:I think its because this line is commented out:in your example, nothing is calling getPage so $json_res is never filledCode: Select all
//$this->getPage($page_id);
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: oop matter
What do you mean? The constructor wont call any methods unless you tell it to, and as getPage is commented out, getPage is never being called. Try it out yourself by adding the following line to your getPage function:devsam wrote:I don't really think this the problem 'cos getPage() function should be called automatically by the constructor when the object is created.
die('This is from getPage');
and from what I can see, that should never happen because it isn't called. Only the constructor/destructor is automatically called - everything else you need to call explicitly yourself.
Re: oop matter
iankent wrote:What do you mean? The constructor wont call any methods unless you tell it to, and as getPage is commented out, getPage is never being called. Try it out yourself by adding the following line to your getPage function:devsam wrote:I don't really think this the problem 'cos getPage() function should be called automatically by the constructor when the object is created.
die('This is from getPage');
and from what I can see, that should never happen because it isn't called. Only the constructor/destructor is automatically called - everything else you need to call explicitly yourself.
I see sorry but this commented by mistake. I tried to call it from the testing.php by myself but this doesn't work either
Re: oop matter
papa wrote:$json_res only exists within your public function getPage($page_id) function.
So either you initiate the var in the constructor or:
echo $p->getPage($page_id);
not really mate, $json_res is declared as public property in the beginning of the class pages.php and been accessed as normal within testing.php after the object is been created so I don't see any problem with that?!!
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: oop matter
devsam wrote:papa wrote:$json_res only exists within your public function getPage($page_id) function.
So either you initiate the var in the constructor or:
echo $p->getPage($page_id);
not really mate, $json_res is declared as public property in the beginning of the class pages.php and been accessed as normal within testing.php after the object is been created so I don't see any problem with that?!!
papa was correct, and not something I'd noticed until a few seconds ago. When you assign the value to $json_res, its creating a local variable within getPage(). Try this instead:
Code: Select all
$this->json_res = JSON_encode($res);hth
Re: oop matter
Noticed, it works, thank you both, Great Devs.
Cheers
Cheers