cnoevil
02-27-2012, 03:03 AM
Greetings,
I'm in the process of trying to learn how to handle objects in php. It seems to be a little trickier than I expected. I've been working with 2 simple classes to try and get the hang of passing one object to a method in another without choking on it. I've finally managed to get that to happen, but my method in class A creates 2 objects even though when I print out a similar call from class B it shows that only 1 object is being passed in. I've put a comment next to the line that produces the issue. I think... Here's the code:
<?php
class A{
public $varA = array(tom,dick,harry);
public function showArray($arr){
echo'<ol>';
foreach($arr as $value){
echo'<li>';
print_r($value);
echo'</li><br/>';}
echo'</ol>';
}
}
class B{
public function getVars($obj){
$this->varB[] = $obj;
return $this->varB; }
}
$objA = new A();
$objB = new B();
echo'<html><head><title>Object Passing Class Test</title></head>';
echo'<body><h1>Passing Objects</h1><br/><br/>';
echo'<pre>';
print_r($objA->varA);
echo'</pre><br/><h3>B::getVars Function</h3><br/><pre>';
print_r($objB->getVars($objA));
echo'</pre><br/><h3>A::showArray Function</h3><pre>';
$objA->showArray($objB->getVars($objA));//<-This call right here produces 2 objects
echo'</pre><br/><br/></body></html>';
What am I missing? Also, does anyone know of some really good in depth tutorials with examples on object wrangling in php that can be found online? I sure would like to get the hang of this sometime in this lifetime.:eek:
Thanks,
Mark
I'm in the process of trying to learn how to handle objects in php. It seems to be a little trickier than I expected. I've been working with 2 simple classes to try and get the hang of passing one object to a method in another without choking on it. I've finally managed to get that to happen, but my method in class A creates 2 objects even though when I print out a similar call from class B it shows that only 1 object is being passed in. I've put a comment next to the line that produces the issue. I think... Here's the code:
<?php
class A{
public $varA = array(tom,dick,harry);
public function showArray($arr){
echo'<ol>';
foreach($arr as $value){
echo'<li>';
print_r($value);
echo'</li><br/>';}
echo'</ol>';
}
}
class B{
public function getVars($obj){
$this->varB[] = $obj;
return $this->varB; }
}
$objA = new A();
$objB = new B();
echo'<html><head><title>Object Passing Class Test</title></head>';
echo'<body><h1>Passing Objects</h1><br/><br/>';
echo'<pre>';
print_r($objA->varA);
echo'</pre><br/><h3>B::getVars Function</h3><br/><pre>';
print_r($objB->getVars($objA));
echo'</pre><br/><h3>A::showArray Function</h3><pre>';
$objA->showArray($objB->getVars($objA));//<-This call right here produces 2 objects
echo'</pre><br/><br/></body></html>';
What am I missing? Also, does anyone know of some really good in depth tutorials with examples on object wrangling in php that can be found online? I sure would like to get the hang of this sometime in this lifetime.:eek:
Thanks,
Mark