gonZoo2
Messages postés2Date d'inscriptionmercredi 28 avril 2004StatutMembreDernière intervention 2 juin 2008
-
2 juin 2008 à 08:13
gonZoo2
Messages postés2Date d'inscriptionmercredi 28 avril 2004StatutMembreDernière intervention 2 juin 2008
-
2 juin 2008 à 23:15
J'ai rencontré un problème donc car je souhaite créer des classes à l'image du langage Java
Voici donc les classes et le code que j'ai réalisé :
object.class.php
<?php
/**
* Object
*
*/
class Object {
function __construct() { }
/**
* Return an array of class' properties
* @access public
* @param string $className optional
* @return array
*/
final static public function getProperties( $className= null ) {
$className $className null ? self::getName() : $className;
$array = get_class_vars( $className );
$tmp = array();
while(list($name, $notUsed) = each($array)) {
$tmp[] = $name;
}
return $tmp;
}
/**
* Return an array of class' methods
* @access public
* @param string/null $classname
* @return array
*/
final static public function getMethods( $className=null ) {
$className $className null ? self::getName() : $className;
$array = get_class_methods( $className );
return $array;
}
/**
* Return the class' name
* @access public
* @return string
*/
static public function getName() {
return __CLASS__;
}
/**
* Assign a value to a property
* @access public
* @param string/array $name
* @param mixed $value
*/
final public function set( $name, $value=null ) {
if(is_array($name)) {
$this->setFromArray($name);
}
else {
if(property_exists($this, $name)) {
$this->$name = $value;
}
else {
throw new Exception('Property doesn\'t exist(' . $name . ')');
}
}
}
/**
* Return a property's value
* @access public
* @param string $name
* @return mixed
*/
final public function get( $name ) {
$return = false;
if(property_exists($this, $name)) {
$return = $this->$name;
}
else {
throw new Exception('Property doesn\'t exist(' . $name . ')');
}
return $return;
}
/**
* Assign a value to several properties from an array
* @access public
* @param array $array
*/
final public function setFromArray( $array ) {
if(is_array($array)) {
while(list($name, $value) = each($array)) {
try {
$this->set($name, $value);
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
}
else {
throw new Exception('Wrong usage of setFromArray($array)');
}
}
/**
* Initialize an object
* @access public
*/
final public function reset() {
$array = get_class_vars($this->getName());
try {
$this->setFromArray( $array );
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
}
?>
singleton.class.php
<?php
/**
* Singleton
*
*/
class Singleton extends Object {
/**
* Instance of Singleton
* @var resource
* @access public
*/
static public $instance = null;
function __construct(){
try {
$this->reset();
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Return the class' name
* @access public
* @return string
*/
static public function getName() {
return __CLASS__;
}
/**
* Return the instance of Class
* @access public
* @return resource
*/
static public function getInstance() {
$className = self::getName();
try {
self::$instance self::$instance null ? new $className() : self::$instance;
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
return self::$instance;
}
}
?>
site.class.php
<?php
/**
* Site
*
*/
class Site extends Singleton {
public $address = null;
public $name = null;
private $path = null;
function __construct(){
try {
parent::__construct();
$this->reset();
$this->setPath();
}
catch(Exception $e) {
//echo $e->getMessage();
throw new Exception($e->getMessage());
}
}
/**
* Return the class' name
* @access public
* @return string
*/
static public function getName() {
return __CLASS__;
}
/**
* Set the path property
* @access private
*/
private function setPath() {
$value = dirname(__FILE__);
try {
$this->set('path', $value);
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
}
?>
Pour la suite :
pare que ton code est vraiment tordu ;-)
Et que tu lis mal la doc, en plus...;-)
'est la méthode Object::set() qui foire :
if(property_exists(get_class($this), $name)) {
property_exists() prend comme 1er argument un nom de classe, PAS un objet.
Ton ode plante juste après de toute manière, sur 'address', ce qui est logique...
A mon avis, tu fais trop compliqué là, tu t'es embrouillé tout seul.