oop matter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devsam
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 2:36 am

oop matter

Post by devsam »

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 ?!!!!!
?>
---------------------------------------------------------------------------------
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: oop matter

Post by iankent »

I think its because this line is commented out:

Code: Select all

//$this->getPage($page_id);
in your example, nothing is calling getPage so $json_res is never filled
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: oop matter

Post by papa »

$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);
devsam
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 2:36 am

Re: oop matter

Post by devsam »

iankent wrote:I think its because this line is commented out:

Code: Select all

//$this->getPage($page_id);
in your example, nothing is calling getPage so $json_res is never filled
I don't really think this the problem 'cos getPage() function should be called automatically by the constructor when the object is created.
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: oop matter

Post by iankent »

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.
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:
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.
devsam
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 2:36 am

Re: oop matter

Post by devsam »

iankent wrote:
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.
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:
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 :(
devsam
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 2:36 am

Re: oop matter

Post by devsam »

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?!!
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: oop matter

Post by iankent »

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);
edit: that tells PHP to assign it to the $json_res variable in the class instead of creating a new, separate local variable for it
hth
devsam
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 2:36 am

Re: oop matter

Post by devsam »

Noticed, it works, thank you both, Great Devs.

Cheers
Post Reply