Pb création class php retour de valeur

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Pb création class php retour de valeur

Re: Pb création class php retour de valeur

par oitsuki » 27 mai 2013, 00:57

Voici ce que j'ai fait, en fait j'ai travaillé sur la class customer ci dessous :
Voici le code

J'ai juste un pb ou met ce code car le mettre là ou il est commentaire, cela pose pb

/*
if ( isset($_SESSION['customer_group_id']) ) {
$this->_data =& $_SESSION['customer_group_id'];
}
*/


echo ' toto : '. $OSCOM_Customer->getCustomersGroupID().'<br />'; est ok

 class customer {
    protected $_is_logged_on = false;
    protected $_data = array();

    public function __construct() {
      if ( isset($_SESSION['customer']) ) {
        $this->_data =& $_SESSION['customer'];
      }

/*
      if ( isset($_SESSION['customer_group_id']) ) {
        $this->_data =& $_SESSION['customer_group_id'];
      }
*/


      if ( isset($this->_data['id']) && is_numeric($this->_data['id']) && ($this->_data['id'] > 0) ) {
        $this->setIsLoggedOn(true);
      }
    }

    public function setIsLoggedOn($state) {
      if ( !is_bool($state) ) {
        $state = false;
      }

      $this->_is_logged_on = $state;
    }

    public function isLoggedOn() {
      return $this->_is_logged_on;
    }

    public function get($key = null) {
      if ( isset($key) ) {
        return $this->_data[$key];
      }

      return $this->_data;
    }

    public function getID() {
      return $this->get('id');
    }

    public function getFirstName() {
      return $this->get('first_name');
    }

    public function getLastName() {
      return $this->get('last_name');
    }

    public function getName() {
      $name = '';

      if ( isset($this->_data['first_name']) ) {
        $name .= $this->_data['first_name'];
      }

      if ( isset($this->_data['last_name']) ) {
        if ( !empty($name) ) {
          $name .= ' ';
        }

        $name .= $this->_data['last_name'];
      }

      return $name;
    }

    public function getGender() {
      return $this->get('gender');
    }

    public function hasEmailAddress() {
      return isset($this->_data['email_address']);
    }

    public function getEmailAddress() {
      return $this->_data['email_address'];
    }

    public function getCountryID() {
      return $this->_data['country_id'];
    }

    public function getZoneID() {
      return $this->_data['zone_id'];
    }

    public function getDefaultAddressID() {
      return $this->_data['default_address_id'];
    }




//B2B
    public function getCustomersGroupID() {

      $customersgroupid = 0;
      if (isset($this->_data['customers_group_id'])) {
        $customersgroupid = $this->_data['customers_group_id'];
      }
      return $customersgroupid;
    }





    public function setData($id) {
      global $OSCOM_PDO;

      $this->_data = array();

      if ( is_numeric($id) && ($id > 0) ) {
        $Qcustomer = $OSCOM_PDO->prepare('select customers_gender, 
                                                 customers_firstname, 
                                                 customers_lastname, 
                                                 customers_email_address, 
                                                 customers_default_address_id 
                                          from :table_customers 
                                          where customers_id = :customers_id'
                                        );
        $Qcustomer->bindInt(':customers_id', $id);
        $Qcustomer->execute();








// B2B
        $QcustomerGroup = $OSCOM_PDO->prepare('select customers_group_id  
                                               from :table_customers
                                               where  customers_id = :customers_id
                                              ');
        $QcustomerGroup->bindInt(':customers_id',  $id );
        $Qcustomer->bindInt(':customers_id', $id);
        $QcustomerGroup->execute();

        if ( $QcustomerGroup->fetch() !== false ) {
          $this->setCustomersGroupID($QcustomerGroup->value('customers_group_id'));
          $_SESSION['customer_group_id'] = $this->_data; 
        }





        if ( $Qcustomer->fetch() !== false ) {
          $this->setIsLoggedOn(true);
          $this->setID($id);
          $this->setGender($Qcustomer->value('customers_gender'));
          $this->setFirstName($Qcustomer->value('customers_firstname'));
          $this->setLastName($Qcustomer->value('customers_lastname'));
          $this->setEmailAddress($Qcustomer->value('customers_email_address'));

          if ( $Qcustomer->valueInt('customers_default_address_id') > 0 ) {
            $Qab = $OSCOM_PDO->prepare('select entry_country_id, 
                                               entry_zone_id 
                                        from :table_address_book 
                                        where address_book_id = :address_book_id 
                                        and customers_id = :customers_id
                                      ');
            $Qab->bindInt(':address_book_id', $Qcustomer->valueInt('customers_default_address_id'));
            $Qab->bindInt(':customers_id', $id);
            $Qab->execute();

            if ( $Qab->fetch() !== false ) {
              $this->setCountryID($Qab->valueInt('entry_country_id'));
              $this->setZoneID($Qab->valueInt('entry_zone_id'));
              $this->setDefaultAddressID($Qcustomer->valueInt('customers_default_address_id'));
            }
          }

          $_SESSION['customer'] = $this->_data;

        }
      }

      return !empty($this->_data);
    }

    public function setID($id) {
      if ( is_numeric($id) && ($id > 0) ) {
        $this->_data['id'] = $id;
      }
    }

    public function setDefaultAddressID($id) {
      if ( is_numeric($id) && ($id > 0) ) {
        $this->_data['default_address_id'] = $id;
      }
    }

    public function hasDefaultAddress() {
      return isset($this->_data['default_address_id']) && is_numeric($this->_data['default_address_id']);
    }

    public function setGender($gender) {
      if ( (strtolower($gender) == 'm') || (strtolower($gender) == 'f') ) {
        $this->_data['gender'] = strtolower($gender);
      }
    }

    public function setFirstName($first_name) {
      $this->_data['first_name'] = $first_name;
    }

    public function setLastName($last_name) {
      $this->_data['last_name'] = $last_name;
    }

    public function setEmailAddress($email_address) {
      $this->_data['email_address'] = $email_address;
    }

    public function setCountryID($id) {
      $this->_data['country_id'] = $id;
    }

    public function setZoneID($id) {
      $this->_data['zone_id'] = $id;
    }





// B2B
    public function setCustomersgroupID($id) {
      $this->_data['customers_group_id'] = $id;
    }





    public function reset() {
      $this->_is_logged_on = false;
      $this->_data = array();

      if ( isset($_SESSION['customer']) ) {
        unset($_SESSION['customer']);
      }




// B2B
      if ( isset($_SESSION['customer_group_id']) ) {
        unset($_SESSION['customer_group_id']);
      }






    }
  }

Re: Pb création class php retour de valeur

par oitsuki » 27 mai 2013, 00:19

Merci pour ton exemple et ton temps.
Pour info, j'ai essayé de dupliquer cette classe qui concerne uniquement le client.
je sais que la duplication de code n'est pas la meilleure solution mais c'est pour mieux comprendre, est ce le bon chemin ?????
note : pour les requêtes sql, elles fonctionnent très bien.


bref,

Voici sur quoi je me suis basé.
<?php
  class customer {
    protected $_is_logged_on = false;
    protected $_data = array();

    public function __construct() {
      if ( isset($_SESSION['customer']) ) {
        $this->_data =& $_SESSION['customer'];
      }

      if ( isset($this->_data['id']) && is_numeric($this->_data['id']) && ($this->_data['id'] > 0) ) {
        $this->setIsLoggedOn(true);
      }
    }

    public function setIsLoggedOn($state) {
      if ( !is_bool($state) ) {
        $state = false;
      }

      $this->_is_logged_on = $state;
    }

    public function isLoggedOn() {
      return $this->_is_logged_on;
    }

    public function get($key = null) {
      if ( isset($key) ) {
        return $this->_data[$key];
      }

      return $this->_data;
    }

    public function getID() {
      return $this->get('id');
    }

    public function getFirstName() {
      return $this->get('first_name');
    }

    public function getLastName() {
      return $this->get('last_name');
    }

    public function getName() {
      $name = '';

      if ( isset($this->_data['first_name']) ) {
        $name .= $this->_data['first_name'];
      }

      if ( isset($this->_data['last_name']) ) {
        if ( !empty($name) ) {
          $name .= ' ';
        }

        $name .= $this->_data['last_name'];
      }

      return $name;
    }

    public function getGender() {
      return $this->get('gender');
    }

    public function hasEmailAddress() {
      return isset($this->_data['email_address']);
    }

    public function getEmailAddress() {
      return $this->_data['email_address'];
    }

    public function getCountryID() {
      return $this->_data['country_id'];
    }

    public function getZoneID() {
      return $this->_data['zone_id'];
    }

    public function getDefaultAddressID() {
      return $this->_data['default_address_id'];
    }

    public function setData($id) {
      global $OSCOM_PDO;

      $this->_data = array();

      if ( is_numeric($id) && ($id > 0) ) {
        $Qcustomer = $OSCOM_PDO->prepare('select customers_gender, 
                                                 customers_firstname, 
                                                 customers_lastname, 
                                                 customers_email_address, 
                                                 customers_default_address_id 
                                          from :table_customers 
                                          where customers_id = :customers_id'
                                        );
        $Qcustomer->bindInt(':customers_id', $id);
        $Qcustomer->execute();

        if ( $Qcustomer->fetch() !== false ) {
          $this->setIsLoggedOn(true);
          $this->setID($id);
          $this->setGender($Qcustomer->value('customers_gender'));
          $this->setFirstName($Qcustomer->value('customers_firstname'));
          $this->setLastName($Qcustomer->value('customers_lastname'));
          $this->setEmailAddress($Qcustomer->value('customers_email_address'));

          if ( $Qcustomer->valueInt('customers_default_address_id') > 0 ) {
            $Qab = $OSCOM_PDO->prepare('select entry_country_id, 
                                               entry_zone_id 
                                        from :table_address_book 
                                        where address_book_id = :address_book_id 
                                        and customers_id = :customers_id
                                      ');
            $Qab->bindInt(':address_book_id', $Qcustomer->valueInt('customers_default_address_id'));
            $Qab->bindInt(':customers_id', $id);
            $Qab->execute();

            if ( $Qab->fetch() !== false ) {
              $this->setCountryID($Qab->valueInt('entry_country_id'));
              $this->setZoneID($Qab->valueInt('entry_zone_id'));
              $this->setDefaultAddressID($Qcustomer->valueInt('customers_default_address_id'));
            }
          }

          $_SESSION['customer'] = $this->_data;
        }
      }

      return !empty($this->_data);
    }

    public function setID($id) {
      if ( is_numeric($id) && ($id > 0) ) {
        $this->_data['id'] = $id;
      }
    }

    public function setDefaultAddressID($id) {
      if ( is_numeric($id) && ($id > 0) ) {
        $this->_data['default_address_id'] = $id;
      }
    }

    public function hasDefaultAddress() {
      return isset($this->_data['default_address_id']) && is_numeric($this->_data['default_address_id']);
    }

    public function setGender($gender) {
      if ( (strtolower($gender) == 'm') || (strtolower($gender) == 'f') ) {
        $this->_data['gender'] = strtolower($gender);
      }
    }

    public function setFirstName($first_name) {
      $this->_data['first_name'] = $first_name;
    }

    public function setLastName($last_name) {
      $this->_data['last_name'] = $last_name;
    }

    public function setEmailAddress($email_address) {
      $this->_data['email_address'] = $email_address;
    }

    public function setCountryID($id) {
      $this->_data['country_id'] = $id;
    }

    public function setZoneID($id) {
      $this->_data['zone_id'] = $id;
    }

    public function reset() {
      $this->_is_logged_on = false;
      $this->_data = array();

      if ( isset($_SESSION['customer']) ) {
        unset($_SESSION['customer']);
      }
    }
  }
?>

Re: Pb création class php retour de valeur

par moogli » 26 mai 2013, 22:51

salut,

J'ai regardé le code rapidement et je dit non :)

Le mot clef global ne devrais pas exister (une classe c'est un objet et un objet doit être "étanche" tous ce qu'il utilise lui est fournis par le constructeur ou un "setter").
si tu a besoin d'une ressource utilise un setter.

la classe que tu indique est une dao, mais ça part dans tout les sens, les setter et getter ne doivent pas contenir "d'intelligence" ils retournent simplement une données.
Certaines méthodes sont redondantes, d'autre inutile, il faut structurer les classes.
regarde des tutos sur uml pour cela :)

je ferais ton code dans ce style (en gardant ta classe)
<?php
class customerGroup {
    protected $is_logged_on = false;
    protected $data = array();
    protected $OSCOM_PDO;
    protected $OSCOM_Customer;

    public function __construct() {
        if (isset($_SESSION['customer_group_id'])) {
            $this->data = $_SESSION['customer_group_id'];
        }
    }

    public function get($key = null) {
        if (isset($this->data[$key])) {
            return $this->data[$key];
        }
        return null; //$this->data; // ? à mon avis comportement bizzar la !
    }

    public function getCustomersGroupId() {
        if (isset($this->data['customers_group_id'])) {
            return $this->data['customers_group_id'];
        } else {
            return 0;
        }
    }

    public function setData($id) {
        $this->data = array();

        if (is_numeric($id) && ($id > 0)) {
// :table_customer ne doit surement pas exister ?
            $QcustomerGroup = $this->OSCOM_PDO->query('select customers_group_id
                                                        from table_customers
                                                        where  customers_id =  ' . $this->OSCOM_PDO->quote($id, PDO::PARAM_INT));
            if ($QcustomerGroup !== false) {
                if ($data = $QcustomerGroup->fetch(PDO::FETCH_ASSOC) !== false) {
                    $this->setID($id);
//                    $this->setCustomerGroupId($QcustomerGroup->value('customers_group_id')); pas de méthode value sur un pdostatement
                    $this->setCustomerGroupId($data['customers_group_id']);
                    $_SESSION['customer_group_id'] = $this->get('customers_group_id'); // non pas  $this->data c'est un tableau !
                    return true;
                }
                return false;
            }
        } // end is_numeric($id)
        return false; // si erreur
    }


    public function setID($id) {
        if (is_numeric($id) && ($id > 0)) {
            $this->data['id'] = $id;
        }
    }

    public function setCustomerGroupId($id) {
        if (is_numeric($id) && ($id > 0)) {
            $this->data['customers_group_id'] = $id;
        }
    }

    public function reset() {
        $this->is_logged_on = false;
        $this->data = array();
        if (isset($_SESSION['customer_group_id'])) {
            unset($_SESSION['customer_group_id']);
        }
    }

    /**
     * @param boolean $is_logged_on
     */
    public function setIsLoggedOn($is_logged_on) {
        $this->is_logged_on = $is_logged_on;
    }

    /**
     * @return boolean
     */
    public function isLoggedOn() {
        return $this->is_logged_on;
    }

    /**
     * @param mixed $OSCOM_Customer
     */
    public function setOSCOMCustomer($OSCOM_Customer) {
        $this->OSCOM_Customer = $OSCOM_Customer;
    }

    /**
     * @return mixed
     */
    public function getOSCOMCustomer() {
        return $this->OSCOM_Customer;
    }

    /**
     * @param mixed $OSCOM_PDO
     */
    public function setOSCOMPDO($OSCOM_PDO) {
        $this->OSCOM_PDO = $OSCOM_PDO;
    }

    /**
     * @return mixed
     */
    public function getOSCOMPDO() {
        return $this->OSCOM_PDO;
    }
    
}

 
sinon perso je ferais une classe customer qui aura une propriété customer_group_id (entre autre) et une classe customuerDao qui elle fera toutes la logique permettant de retourner les infos d'un "customer" et retourner un "customer" avec toute les infos le concernant.


@+

Pb création class php retour de valeur

par oitsuki » 26 mai 2013, 17:59

Bonjour,

J'essaye de réaliser une classe ou en fonction de mon id groupe client de ma table client retourne la valeur correcte
si je ne suis pas connecté : customergroupid = 0
si je suis connecté mais le client est normal : customergroupid = 0
si je suis connecté mais le client est revendeur par exemple : customergroupid = 1

voici ma classe elle retourne tj la valeur = 0 : ou est l'erreur ?

Merci de votre aide car je ne vois pas ou est l'erreur quand je fais

echo 'toto' . $OSCOM_CustomerGroup->getID();

Je suis très débutant.

  class customerGroup {
    protected $_is_logged_on = false;
    protected $_data = array();

    public function __construct() {
      if ( isset($_SESSION['customer_group_id']) ) {
        $this->_data =& $_SESSION['customer_group_id'];
      }

      if ( isset($this->_data['id']) && is_numeric($this->_data['id']) && ($this->_data['id'] > 0) ) {
        $this->isLoggedOn();
      }
    }


    public function isLoggedOn() {
      return $this->_is_logged_on;
    }

    public function get($key = null) {

      if ( isset($key) ) {
        return $this->_data[$key];
      }

      return $this->_data;
    }

    public function getID() {
      return $this->get('id');
    }


    public function getCustomersGroupId() {

      $customersGroupId = 0;

      if ( isset($this->_data['customers_group_id']) > 0 ) {
        $customersGroupId .=  $this->_data['customers_group_id'];
      }

      return $customersGroupId;
    }

    public function setData($id) {

      global $OSCOM_PDO, $OSCOM_Customer;

      $this->_data = array();

      if ( is_numeric($id) && ($id > 0) ) {
//        $customersGroupId = array(array('id' => '0', 'text' => 0) );

        $QcustomerGroup = $OSCOM_PDO->prepare('select customers_group_id  
                                               from :table_customers
                                               where  customers_id =  :customers_id
                                              ');
        $QcustomerGroup->bindInt(':customers_id',  (int)$OSCOM_Customer->getID() );
//        $Qcustomer->bindInt(':customers_id', $id);
        $QcustomerGroup->execute();

        if ( $QcustomerGroup->fetch() !== false ) {
          $this->isLoggedOn();
          $this->setID($id);        
          $this->setCustomerGroupId($QcustomerGroup->value('customers_group_id'));
        }

        $_SESSION['customer_group_id'] = $this->_data;
      } // end is_numeric($id)

      return !empty($this->_data);
    }


    public function setID($id) {
      if ( is_numeric($id) && ($id > 0) ) {
        $this->_data['id'] = $id;
      }
    }    

    public function setCustomerGroupId($id) {
      if ( is_numeric($id) && ($id > 0) ) {
        $this->_data['customers_group_id'] = $id;
      }
    }

    public function reset() {
      $this->_is_logged_on = false;
      $this->_data = array();

      if ( isset($_SESSION['customer_group_id']) ) {
        unset($_SESSION['customer_group_id']);
      }
    }
  }