Overloading __set() ..............

admin

Administrator
Staff member
Hi.
I never used overloading in php 5 but I think
this is a case I need it ;)


<?php
error_reporting(E_ALL | E_STRICT);
class FileException extends Exception{
public function __construct($message,$code=0){
parent::__construct($message, $code);
}
public function __toString(){
return " ".__CLASS__ . "<br /> Line : <b>[{$this->line}]</b><br /> Code :<b>[{$this->code}]</b><br /> Msg : {$this->message}<br /> File : <b>{$this->file}</b>\n";
}
}
class InvalidArgException extends Exception{
public function __construct($message, $code = 0){
parent::__construct($message, $code);
}
public function __toString(){
return " ".__CLASS__ . "<br /> Line : <b>[{$this->line}]</b><br /> Code :<b>[{$this->code}]</b><br /> Msg : {$this->message}<br /> File : <b>{$this->file}</b>\n";
}
}
class Rss2{
private $doc= null;
private $root= null;
private $channel= null;
private $feed= '';
private $optional= array('language'=>'',
'copyright'=>'',
'managingEditor'=>'',
'webMaster'=>'',
'pubDate'=>'',
'lastBuildDate'=>'',
'category'=>'',
'generator'=>'',
'docs'=>'',
'cloud'=>'',
'ttl'=>'',
'image'=>'',
'rating'=>'',
'textInput'=>'',
'skipHours'=>'',
'skipDays'=>''
);
private $required= array();
public function __construct(){
$this->doc= new DOMDocument('1.0', 'UTF-8');
$this->doc->formatOutput= true;
$this->root= $this->doc->createElement('rss');
$this->root->setAttribute('version','2.0');
$this->channel= $this->doc->createElement('channel');
}
public function __set($nm, $val){
if (!isset($this->optional[$nm])){
throw new InvalidArgException('Invalid parameter.The referenced element is not valid<b>['.__CLASS__.']</b>');
}
$this->optional[$nm] = $val;
}
public function setFeed($feed='feed.xml'){
$this->feed= $feed;
}
private function buildOptional(){
foreach($this->optional as $key => $value){
if(!empty($this->optional[$key])){
$nodespace= $this->doc->createElement($key);
$nodetext= $this->doc->createTextNode($value);
$nodespace->appendChild($nodetext);
$this->channel->appendChild($nodespace);
}
}
}
public function setRss2(){
$this->buildOptional();
$this->root->appendChild($this->channel);
$this->doc->appendChild($this->root);
}
public function save(){
if(!$this->doc->save($this->feed)){
throw new FileException('Error saving file ['.$this->feed.'] in class <b>['.__CLASS__.']</b>');
}
}
public function saveXML(){
if(!$xml=$this->doc->saveXML()){
throw new Exception('Error build string in class <b>['.__CLASS__.']</b>');
}
return $xml;
}

}
try{
//date_default_timezone_set('Europe/Rome');
$rss2= new Rss2();
$rss2->language= 'en-us';
$rss2->pubDate= date("D, M j G:i:s Z Y");
$rss2->lastBuildDate= date("D, M j G:i:s Z Y");
$rss2->setRss2();
echo $rss2->saveXML();
}
catch(InvalidArgException $e){
echo $e;
}
catch(InvalidArgException $e){
echo $e;
}
catch(Exception $e){
echo $e->getMessage();
}
?>


Any comments on how I should be doing this better would be gratefully appreciated :o


Best wishes.Nothing wrong with the way you are using __set(); It's quite a common pattern.

I don't know enough about rss to over a useful opinion on the rest of the class. Seems reasonable to me.

Wonder if Whisher ever tried to fly by jumping off a building?Nothing wrong with the way you are using __set(); It's quite a common pattern.

I don't know enough about rss to over a useful opinion on the rest of the class. Seems reasonable to me.

Wonder if Whisher ever tried to fly by jumping off a building?


:D
 
Back
Top