NYCPHP Meetup

NYPHP.org

[nycphp-talk] Object Methods and Properties

Joe Crawford jcrawford at codebowl.com
Thu Jul 29 12:29:49 EDT 2004


Something is still wrong with this code

	function setPart($part, $val, $append=FALSE) {
		if (in_array($part, get_class_vars(get_class($this)))) {
			if(($part) && ($val)) {
				if ($append) $this->{'_' . $part} .= $val;
				else $this->{'_' .$part} = $val;
			}
			return true;
		}
		$this->sendError('undefined property.');
	}



this code is still allowing properties to be set that have not been
defined :(

here is my entire class


<?
include_once('smarty/Smarty.class.php');
class template extends Smarty {

	private $_header;
	private $_body;
	private $_footer;

	function template() {
		$this->_header = null;
		$this->_body = null;
		$this->_footer = null;

		$this->Smarty();

		$this->template_dir = BASE_DIR.'/smarty/dfs/templates/';
		$this->compile_dir = BASE_DIR.'/smarty/dfs/templates_c/';
		$this->config_dir = BASE_DIR.'/smarty/dfs/configs/';
		$this->cache_dir = BASE_DIR.'/smarty/dfs/cache/';

		$this->caching = false;
		$this->assign('app_name','Developer Financial Software');
	}

	function header($val) {
		if($val) {
			$this->_header = $val;
		} else {
			return $this->_header;
		}
	}

	function body($val) {
		if($val) {
			$this->_body = $val;
		} else {
			return $this->_body;
		}
	}

	function footer($val) {
		if($val) {
			$this->_footer .= $val;
		} else {
			return $this->_footer;
		}
	}

	function setPart($part, $val, $append=FALSE) {
		if (in_array($part, get_class_vars(get_class($this)))) {
			if(($part) && ($val)) {
				if ($append) $this->{'_' . $part} .= $val;
				else $this->{'_' .$part} = $val;
			}
			return true;
		}
		$this->sendError('undefined property.');
	}
	
	function sendError($err) {
		if($err) {
			echo $err;	
			exit;
		}	
	}

	function displayError($live, $page) {
		if($page) {
			if($live) {
				$this->display('error/static/'.$page.'.tpl');
			} else {
				$this->display('error/'.$page.'.tpl');
			}
		} else {
			$this->display('error/static/generic.tpl');
		}
	}

	function displayPage($page) {
		$this->assign('header', $this->_header);
		$this->assign('body', $this->_body);
		$this->assign('footer', $this->_footer);
		$this->display($page.'.tpl');
	}
}
?>

here is my index.php page

<?
include_once('include/global.php');

$page->subTitle('Home');
$tpl->setPart('header', $tpl->fetch('header.tpl'));
$tpl->setPart('body', $tpl->fetch('main.tpl'));
$tpl->setPart('footer', $tpl->fetch('footer.tpl'));
$tpl->setPart('test', 'this is a test');
echo $tpl->_test;

$tpl->displayPage('page');

?>


when the page is executed it echo's the value of $tpl->_test when in
fact _test should not be set since i have no var $_test; in my class
definition.

anyone here that can shed some light on this problem?

Joe Crawford Jr.



On Wed, 2004-07-28 at 21:33, Joe Crawford wrote:
> Dan,
> 
> i do not want the class setting any undefined properties.
> 
> Joe Crawford Jr.
> 
> 
> On Wed, 2004-07-28 at 00:16, Dan Cech wrote:
> > Phillip Powell wrote:
> > > Joe Crawford wrote:
> > > 
> > >> Phill,
> > >>
> > >> thanks that worked just as expected...
> > >>
> > >> Now anyone know of a way to make sure $part is actually one of the class
> > >> variables?
> > 
> > > 
> > > if (in_array($part, get_class_vars(get_class($this)))) { // DO STUFF }
> > > 
> > > Assuming you have instantiated a MyClass object prior to this line
> > 
> > Actually you would be better off with:
> > 
> > function setPart($part = NULL, $val = NULL, $append = FALSE) {
> >    if (isset($part) && isset($val) && @isset($this->{'_'.$part})) {
> >      if ($append) {
> >        $this->{'_'.$part} .= $val;
> >      } else {
> >        $this->{'_'.$part} = $val;
> >      }
> >      return TRUE;
> >    }
> >    return FALSE;
> > }
> > 
> > There is no need to go through the added overhead of calling 
> > get_class_vars and get_class, unless you want to disallow setting any 
> > vars not defined in the class definition.
> > 
> > Using the @isset(..) method you could define an addPart and delPart 
> > function if required, which you can not do with Phil's solution.
> > 
> > Dan
> > 
> > _______________________________________________
> > New York PHP Talk
> > Supporting AMP Technology (Apache/MySQL/PHP)
> > http://lists.nyphp.org/mailman/listinfo/talk
> > http://www.newyorkphp.org
> > 
> > 
> 
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
> 
> 




More information about the talk mailing list