Re: __get, __set, __clone

From: Date: Wed, 09 Jul 2003 02:13:37 +0000
Subject: Re: __get, __set, __clone
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-154469@lists.php.net to get a copy of this message
Hi Yann, What you are trying to do is possible in PHP 4, actually: <?php class MyTester {
    function MyTester()
    {
        $this->var_name1 = "value1";
        $this->var_name2 = "value2";
    }
} $test = new MyTester; var_dump($test); // shows two variables, $var_name1 and $var_name2 ?> Here is sample code that uses __set to allow you to transparently declare an object that allows you to set database values just by setting properties. <?php class UsesSetForMysql {
    private $_db;
    private $_table;
    private $_row;
    private $_keyname;
    function __construct($server, $user, $password, $database, $table, $keyname, $keyvalue)
    {
         $this->_db = mysql_connect($server, $user, $password);
         $this->_keyname = $keyname;
         $this->_table = $table;
         if ($this->_db) {
             $test = mysql_select_db($this->_db, $database);
             if ($test) {
                  $a = mysql_query('SELECT * FROM ' . $this->_table . ' WHERE ' . $this->_keyname . ' = "' . $keyvalue . '"');
                  if ($a) {
                       $this->_row = mysql_fetch_array($a, MYSQL_ASSOC);
                  }
             } else {
                  mysql_close($this->_db);
                  $this->_db = false;
             }
         }
    }
    function __destruct()
    {
         if ($this->_db) {
              mysql_close($this->_db);
              $this->_db = false;
         }
    }
    function __set($name, $value)
    {
         if (isset($this->_row) && $this->_row) {
             $this->_row[$name] = $value;
             $a = mysql_query('UPDATE ' . $this->_table . " SET $name = \"$value\" WHERE " . $this->_keyname . ' = "' . $this->_row[$this->_keyname] . '"');
             if ($a) {
                  $keyvalue = $this->_row[$name];
                  if ($name == $this->_keyname) {
                       $keyvalue = $value;
                  }
                  $a = mysql_query('SELECT * FROM ' . $this->_table . ' WHERE ' . $this->_keyname . ' = "' . $keyvalue . '"');
                  $this->_row = mysql_fetch_array($a, MYSQL_ASSOC);
             }
         }
    }
    function __get($name, &$value)
    {
        if (isset($this->_row) && is_array($this->_row)) {
            if (isset($this->_row[$name])) {
                $value = $this->_row[$name];
                return true;
            } else {
                return false;
            }
        }
    }
} $table = new UsesSetForMysql('localhost', 'dummy', 'madeup', 'mytable', 'mykey', 6); $table->FirstName = 'Greg'; $table->LastName = 'Beaver'; // etc. ?> :) Greg Yann Larrivee wrote:
Hi, in the past 2 days i have been looking into php5 and it's new features. I am not a psecialiste of OOP (only been OOPing for 2 month) For a moment i tought that __set would allow me not to have to define a methode __set that would set a value to a member variable. It seems like we have to define the class __set and __get. for example i tought i would be able to do. class a{ function __construct(){ $this->__set("var_name1","value1"); $this->__set("var_name2","value2"); } } I read these 2 tutorial http://www.phpbuilder.com/columns/argerich20030411.php3?page=5 (the explanation seems really bad to me and does not show any usefull way to use these functions) http://talks.php.net/show/php5intro/25 This one well i comes back to the same thing as if i code it my self. So my big question is why , when , how do we use these features (__get,__set) For a moment i tought i would save me time, but if i have to redefine them what is the point ? I guess i am somewhat confuse on this issue, any explanation would be appreciated. Thanks


« previous php.general (#154469) next »