I can't seem to get this right.. It seems I keep creating a new stdClass when I'm tryng to add to what should be one for the scope.
class d_elem {
private $el;
function __constructor($p) {
$el = new stdClass;
}
private static function ele_base($p) {
print('ele_base<br/>');
d_elem::ele_base_attr($p);
}
private static function ele_base_attr($p) {
print('ele_base_attr<br/>');
isset($p['id']) ? $el->id = '' . $p['id'] . '' : '';
print_r($p);print('<br/>');
print_r($el);print('<br/>'); //<< should have added the id but created a new one????
}
public static function ele_a($p) {
d_elem::ele_base($p);
isset($p['href']) ? $el->href = '' . $p['href'] . '' : '';
isset($p['TEXT']) ? $el->TEXT = '' . $p['TEXT'] . '' : '';
print_r($p);print('<br/>');
print_r($el);print('<br/>');//<< should have added the id but only has the href and TEXT when all 3 should be there
//skip the retrun
}
}
echo d_elem::ele_a(array('href'=>"#",'TEXT'=>'test','id'=>'test'));
Any one have an idea?
thank you -Jeremy
[EDIT]-------------------------------------- per the suggestion above
class d_elem{
public static $el;
private static function init(){
self::$el = new stdClass;
}
private static function ele_base($p) {
print('ele_base<br/>');
self::ele_base_attr($p);
}
private static function ele_base_attr($p) {
print('ele_base_attr<br/>');
isset($p['id']) ? $el->id = '' . $p['id'] . '' : '';
print_r($el);print('<br/>'); //<< should have added the id but created a new one????
}
public static function ele_a($p) {
self::init();
self::ele_base($p);
isset($p['href']) ? $el->href = '' . $p['href'] . '' : '';
isset($p['TEXT']) ? $el->TEXT = '' . $p['TEXT'] . '' : '';
print_r($el);print('<br/>');
//skip the retrun
}
}
d_elem::ele_a(array('href'=>"#",'TEXT'=>'test','id'=>'test'));
Still produces the same output of
ele_base
ele_base_attr
stdClass Object ( [id] => test )
stdClass Object ( [href] => # [TEXT] => test )
.
and want
stdClass Object ( [id] => test [href] => # [TEXT] => test )
tk -J
[END SOLUTION AS OF YET]
class d_elem {
private static $el; /// fill this as we run from function to function
private static function init(){ // start off but creating the object
self::$el = new stdClass;
}
private static function ele_base($p) {
self::ele_base_attr($p);// here we fill base on some condition.. simple test first
}
private static function ele_base_attr($p) {
isset($p['id']) ? self::$el->id = ' id="' . $p['id'] . '" ' : ''; // this should be pushed to the class level object
}
public static function ele_a($p) {
$p=!is_array($p)?get_object_vars ($p):$p; // make sure that if p is an object we trun it to an array
self::init();// set the class object
self::ele_base($p);// make first add to class object
isset($p['href']) ? self::$el->href = ' href="' . $p['href'] . '" ' : ''; make second add to the class object
foreach (self::$el as $key => $value) {
$ele .= $value; // spit the values back out to return from the class object
}
$ele .= $p['TEXT'] ; // test something to return at this level
return $ele // return all the properties in this case a string of them
}
}
echo d_elem::ele_a(array('href'=>"#",'TEXT'=>'test','id'=>'test')); // call for the output
$el = new stdClass;should be$el = new stdClass();$el = new stdClass;works the same way that$el = new stdClass();does.. but I suppose the latter is the better practice?