[nycphp-talk] Help with a self:: ish problem
Rob Marscher
rmarscher at beaffinitive.com
Thu Dec 6 21:11:10 EST 2007
On Dec 6, 2007, at 9:58 AM, Rick Retzko wrote:
...snip...
> $completed=self::update(); //<== THIS IS THE PROBLEM LINE
...snip...
> When I change that line to "$completed=self::$this-
> >_aData['action'].'()';" (which contains the string 'update'), the
> line is read, but nothing happens.
> When I add "$action=$this->_aData['action'].'()';", then change the
> line to "$completed=self::$action;", I get the following error
> message:
> Fatal error: Access to undeclared static property: actions::$action
> in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\cjmea
> \hs_choir\classes\class.actions.php5 on line 37.
Here's what you're looking for. Also... my two cents... name your
class something less common than actions and capitalize your class
names - this helps with readability and making it clear that something
is a class. Use protected instead of private unless you really need
visibility contained to the current class for some reason. If you
extended your actions class, the new class wouldn't be able to access
the private methods and variables.
<?php
class RetzkoActions
{
protected $_aData;
protected $_table;
public function __construct()
{
$this->_aData = array();
$this->_table = false;
}
protected function update()
{
echo "Update being executed!\n";
return true;
}
public function do_action($table, $data)
{
$this->_aData = $data;
$this->{$this->_aData['action']}();
// as you can see this is hard to read...
// so it's probably worth doing this
$action = $this->_aData['action'];
$this->$action();
}
}
$instance = new RetzkoActions();
$instance->do_action('someTable', array('action' => 'update'));
?>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071206/515598db/attachment.html>
More information about the talk
mailing list