[nycphp-talk] Help with a self:: ish problem
Rick Retzko
rick at click-rick.net
Fri Dec 7 05:49:07 EST 2007
Thanks all! I've got it working now. Great suggestions and truly helpful
references and links.
Best Regards -
Rick
============
rick at click-rick.net
_____
From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
Behalf Of Rob Marscher
Sent: Thursday, December 06, 2007 9:11 PM
To: NYPHP Talk
Subject: Re: [nycphp-talk] Help with a self:: ish problem
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/20071207/e02e6b8a/attachment.html>
More information about the talk
mailing list