php5 object help needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gruessle
Forum Newbie
Posts: 2
Joined: Wed Apr 23, 2008 3:25 pm
Location: Florida

php5 object help needed

Post by Gruessle »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I like to put following if statement inside the class so that I have to only call the class with

Code: Select all

$acl = new Tamer_ACL();
but I can't figure out how to do that.

Code: Select all

 
require_once 'Acl.php';
$acl = new Tamer_ACL();
if($acl->old == TRUE){
    $acl->aclCreate();
    $acl->aclSave($acl);
}else{
    $acl = $acl->aclGet();
}
 
The Class

Code: Select all

<?php
require_once 'Zend/Acl.php';
require_once 'Zend/Acl/Role.php';
require_once 'Zend/Acl/Resource.php';
 
class Tamer_ACL extends Zend_ACL 
{
    protected $aclCfg = 'Acl.php';
    protected $aclTxt = 'acl.txt';
    protected $aclTime = 'time.txt';
    protected $filemtime;
    
    public function __construct()
    {
        if(filemtime($this->aclCfg) != file_get_contents($this->aclTime)){
            $this->old = TRUE;
        }else{
            $this->old = FALSE;
        }
        $this->filemtime = filemtime($this->aclCfg);
        
    }
    
    function aclCreate()
    {
        $this->addRole(new Zend_Acl_Role('guest'));
        //$this->deny('guest', null, 'view');
        
        $this->addRole(new Zend_Acl_Role('member'));
        $this->allow('member', null, array('view'));
        
        // Administrator does not inherit access controls
        $this->addRole(new Zend_Acl_Role('admin'), 'member');
        $this->allow('admin', null, array('add', 'edit', 'delete'));
        
        $this->add(new Zend_Acl_Resource('adminArea'));
        $this->add(new Zend_Acl_Resource('jobsearch'));
    }
    
    public function aclGet()
    {
        return unserialize(file_get_contents($this->aclTxt));
    }
    
    public function aclSave($array)
    {
        file_put_contents($this->aclTxt, serialize($array));
        file_put_contents($this->aclTime, $this->filemtime);
    }
    
    
}
?>

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: php5 object help needed

Post by mattcooper »

Should be as simple as:

Code: Select all

 
<?php
public function __construct()
{
    if(filemtime($this->aclCfg) != file_get_contents($this->aclTime)){
        $this->aclCreate();
        $this->aclSave();
    }else{
        $this->aclGet();
    }
    $this->filemtime = filemtime($this->aclCfg);}
?>
 
Since you're not refering to the value of $this->old at any other point in your class, you can remove its declaration as I have. The control will now execute when you call your class with

Code: Select all

$acl = new Tamer_ACL();
HTH :)
Post Reply