Page 1 of 1

First try at OOPHP

Posted: Tue Jul 07, 2009 2:52 pm
by Korken
Hello!

Here I have my first two classes that I have written in PHP and I'd like to know how I should improve my code.
Any comments will be helpful, thanks!

//Emil
Sweden

Code 1, User class.
The background thought was a simple way to work with logged in users (and different admin levels).

Code: Select all

<?php
/**********************************************************************
 * - Made in the Eclipse IDE: http://www.eclipse.org
 * - Tested on PHP 5.2
 * 
 * Usage of the class User
 * 
 * Attention!: user_id is a string and admin_level is an integer.
 * 
 * -> On creation of the object:
 * You can specify User ID and Admin Level when creating the object.
 * Ex: $user_holder = new User("user_id", admin_level);
 * 
 * -> SetUserID("user_id"):
 * This will set/change the User ID.
 * 
 * -> SetAdminLevel(admin_level):
 * This will set/change the Admin Level.
 * 
 * -> GetUserID():
 * This will return a string with the User ID.
 * 
 * -> GetAdminLevel():
 * This will return an integer with the Admin Level.
 * 
 **********************************************************************/
 
class User
{
    private $user_id;
    private $admin_level;
    
    public function __construct($uid, $alvl = 0)
    {
        $this->SetUserID($uid);
        $this->SetAdminLevel($alvl);
    }
    
    public function SetUserID($uid)
    {
        if ((string)$uid != "")
            $this->user_id = (string)$uid;
    }
    
    public function SetAdminLevel($alvl)
    {
        if ((int)$alvl >= 0)
            $this->admin_level = (int)$alvl;
    }
    
    public function GetUserID()
    {
        return $this->user_id;
    }
    
    public function GetAdminLevel()
    {
        return $this->admin_level;
    }
}
?>

Code 2, a simple shoppingcart class:

Code: Select all

<?php
/**********************************************************************
 * - Made in the Eclipse IDE: http://www.eclipse.org
 * - Tested on PHP 5.2
 * 
 * Usage of the class ShoppingCart
 * 
 * Attention!: product_id is a string and value is an integer.
 * 
 * -> AddItem("product_id"):
 * This will add an item to the cart.
 * If the item already exists, the quantity of that item will increased by 1.
 * 
 * -> DeleteItem("product_id"):
 * This will remove an item from the cart.
 * Remove as in it will be gone, if you want to change the quantity see 'SetQuantity'.
 * 
 * -> GetItems():
 * This will return an array with all the items in the cart.
 * 
 * -> SetQuantity("product_id", value):
 * This will change the quantity of the item to value.
 * If value is 0 the item will be automatically removed.
 * 
 * -> GetQuantity("product_id"):
 * This will return the quantity of the item.
 * 
 * 
 * Design of the cart array:
 *  _______________________
 * | Product ID | Quantity |
 * |  (string)  |  (int)   |
 * |------------|----------|
 * | exampleid1 |    5     |
 * |- - - - - - | - - - - -|
 * | exampleid2 |    3     |
 * |- - - - - - | - - - - -|
 * | exampleid5 |    13    |
 *  -----------------------
 **********************************************************************/
 
class ShoppingCart
{
    private $cart = array();
    
    public function AddItem($product_id)
    {
        if (array_key_exists($product_id, $this->cart))
            $this->cart[$product_id] += 1;
        else
            $this->cart[$product_id] = 1;
    }
    
    public function DeleteItem($product_id)
    {
        if (array_key_exists($product_id, $this->cart))
            unset($this->cart[$product_id]);
    }
    
    public function GetItems()
    {
        return array_keys($this->cart);
    }
    
    public function SetQuantity($product_id, $value)
    {
        if (array_key_exists($product_id, $this->cart))
        {
            if ((int)$value > 0)
                $this->cart[$product_id] = (int)$value;
            else
                unset($this->cart[$product_id]);
        }
    }
    
    public function GetQuantity($product_id)
    {
        if (array_key_exists($product_id, $this->cart))
            return $this->cart[$product_id];
    }
}
?>

Re: First try at OOPHP

Posted: Tue Jul 07, 2009 4:31 pm
by Christopher
It is certainly a start, although your class are just Value Objects. In PHP they are little better than arrays. Also, camelCase() is the PHP standard.

Re: First try at OOPHP

Posted: Tue Jul 07, 2009 5:17 pm
by Korken
Thanks for the comment!
I forgot about camel case, but I have fixed that now, how ever I don't like using it for class names.

I have started on a MySQL Adapter, and it's not just values. It may give better insight into my coding habits.
(Sorry for the strange way I put the comments at the top, but I understand it better than rather to look for comments in the code.)

Code: Select all

<?php
/**********************************************************************
 * - Made in the Eclipse IDE: http://www.eclipse.org
 * - Tested on PHP 5.2
 * 
 * Usage of the class MySQLAdapter
 * 
 * Attention!: "word"/'word' are strings, without the " or the ' are ints and anything with array in its name is an array.
 * 
 * -> On creation of the object:
 * You must specify the connection array.
 * Ex: $db_con = new MySQLAdapter(array('host' => 'localhost',
 *                                      'db' => 'my_database',
 *                                      'username' => 'my_username',
 *                                      'password' => 'my_pass'));
 * 
 * -> connect():
 * This will connect to the database.
 * Don't forget to close the connection when you are done.
 * 
 * -> disconnect():
 * This will close the connection to the database.
 * 
 * -> escapeString("string"):
 * This will return the string ready for insert into/update a database.
 * 
 * -> numRows("table"):
 * This will return the number of row in a table.
 * 
 * -> query("SQL string");
 * This will send a SQL string to the database and return the result.
 * 
 * -> fetchRow("result"):
 * This will return a mysql_fetch_row from "result".
 * If "result" is unspecified it will return from the last Query.
 * 
 * -> fetchArray("result"):
 * This will return a mysql_fetch_array from "result".
 * If "result" is unspecified it will return from the last Query.
 * 
 * -> insert("table", data_array):
 * This will insert data into the database.
 * Ex: $db_con->insert("users", array('NICK' => 'my_nick',
 *                                    'ADMIN' => '1',
 *                                    'PASSWORD' => 'my_pass'));
 *                                    
 * SQL string produced: "INSERT INTO users(NICK, ADMIN, PASSWORD) VALUES('my_nick', '1', 'my_pass')" 
 * 
 * Attention: NOW() and NULL works.
 * Ex: $db_con->insert("users", array('NICK' => 'my_nick',
 *                                    'ADMIN' => 'NULL',
 *                                    'PASSWORD' => 'my_pass',
 *                                    'DATE' => 'NOW()'));
 *                                    
 * SQL string produced: "INSERT INTO users(NICK, ADMIN, PASSWORD, DATE) VALUES('my_nick', NULL, 'my_pass', NOW())"
 * 
 * 
 * -> update("table", data_array, "where"):
 * This will update the database.
 * The string "where" is where you want to do the update, ex: ID = 2
 * Ex: $db_con->update("users", array('NICK' => 'my_new_nick',
 *                                    'PASSWORD' => 'my_new_pass'), "ID = 12 LIMIT 1");
 *                                    
 * SQL string produced: "UPDATE users SET NICK = 'my_new_nick', PASSWORD = 'my_new_pass' WHERE ID = 12 LIMIT 1"
 * Attention: NOW() and NULL works.
 *
 * 
 * -> delete("table", "where"):
 * This will delete a post from a database.
 * Ex: $db_con->delete("users", "ID = 7");
 * 
 * SQL string produced: "DELETE FROM users WHERE ID = 7"
 * 
 * 
 * 
 * Connection array design:
 *  __________________________
 * |     Key    |    Value    |
 * |  (string)  |   (string)  |
 * |------------|-------------|
 * |    host    |   my_host   |
 * |- - - - - - | - - - - - - |
 * |     db     |     my_db   |
 * |- - - - - - | - - - - - - |
 * |  username  | my_username |
 * |- - - - - - | - - - - - - |
 * |  password  | my_password |
 *  --------------------------
 *  
 * Insert and Update array design (example):
 *  __________________________
 * |  TableCol  |    Value    |
 * |  (string)  |   (string)  |
 * |------------|-------------|
 * |    NICK    |   my_nick   |
 * |- - - - - - | - - - - - - |
 * |   ADMIN    |      1      |
 * |- - - - - - | - - - - - - |
 * |  PASSWORD  | my_password |
 *  --------------------------
 **********************************************************************/
 
class MySQLAdapter
{   
    private $host;
    private $db;
    private $username;
    private $password;
    
    private $db_link;
    private $db_result;
    
    public function __construct($array)
    {
        if (isset($array['host']) || isset($array['db']) || isset($array['username']) || isset($array['password']))
            exit("Error: There are invalid connection properties.");
        
        $this->host = $array['host'];
        $this->db = $array['db'];
        $this->username = $array['username'];
        $this->password = $array['password'];
    }
    
    public function connect()
    {
        $this->db_link = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
        mysql_select_db($this->db) or die(mysql_error());
        
        return $this->db_link;
    }
    
    public function disconnect()
    {
        mysql_close($this->db_link);    
    }
    
    public function query($db_sql)
    {
        $this->db_result = mysql_query($db_sql) or die(mysql_error());
        return $this->db_result;
    }
    
    public function insert($table, $array)
    {
        $this->query(create_insert_sql_string($table, $array));
    }
    
    public function update($table, $array, $where)
    {
        $this->query(create_update_sql_string($table, $array, $where));
    }
    
    public function delete($table, $where)
    {
        $this->query("DELETE FROM $table WHERE $where");
    }
    
    public function fetchRow($result = null)
    {
        if (!$result)
             $result = $this->db_result;
             
        return mysql_fetch_row($result);
    }
    
    public function fetchArray($result = null)
    {
        if (!$result)
             $result = $this->db_result;
             
        return mysql_fetch_array($result);
    }
    
    public function escapeString($string)
    {
        if(get_magic_quotes_gpc())
            $string = stripslashes($string);
            
        return mysql_real_escape_string($string);
    }
 
    public function numRows($table)
    {
        $row = mysql_fetch_array($this->query("SELECT COUNT(*) AS numrows FROM $table"), MYSQL_ASSOC);
        return (int)$row['numrows'];
    }
    
    private function create_insert_sql_string($table, $array)
    {
        $place = "";
        $values = "";
        
        foreach ($array as $key=>$val)
        {
            $place .= "$key, ";
            
            if (strtolower($val) == "null")
                $values .= "NULL, ";
            elseif (strtolower($val) == "now()")
                $values .= "NOW(), ";
            else
                $values .= "'" . $val . "', ";
        }
        
        $place = substr_replace($place, "", -2);
        $values = substr_replace($values, "", -2);
        return "INSERT INTO $table($place) VALUES($values)";
    }
    
    private function create_update_sql_string($table, $array, $where)
    {
        $str = "";
        
        foreach ($array as $key=>$val)
        {
            $str .= "$key = ";
            
            if (strtolower($val) == "null")
                $str .= "NULL, ";
            elseif (strtolower($val) == "now()")
                $str .= "NOW(), ";
            else
                $str .= "'" . $val . "', "; 
        }
        
        $str = substr_replace($str, "", -2);
        return "UPDATE $table SET $str WHERE $where";
    }
}
?>

Re: First try at OOPHP

Posted: Tue Jul 07, 2009 6:28 pm
by Christopher
You could possibly split that into a couple of classes. The methods that deal with the result of a query could be its own class which could implement Iterator. The insert/update/delete could be a separate class that extends the base connection class. It has a Gateway style interface. Or you could keep them combined if you like.

Re: First try at OOPHP

Posted: Tue Jul 14, 2009 7:19 am
by Korken
Hmm, I'm not quite sure what you mean. Could you give an example?

Thanks!

//Emil

Re: First try at OOPHP

Posted: Tue Jul 14, 2009 2:31 pm
by pickle
For your User class, you don't need separate set*() and get*() functions - look into the magical __get() and __set() functions. This will allow the class to grow easier in the future.

For your MySQL class:
- I'd look at wrapping mysqli rather than mysql. MySQLi is a little nicer to work with. I'd also suggest thinking about calling the connect() function right from the constructor. In my experience, I've never needed to create an instance of my DB wrapper class, without needing to connect to the database, so I just eliminated that one extra step.
- It might be more manageable if, rather than calling die() all over the place, you just stored the error in your object and returned FALSE. You would then have greater control over program flow and exactly how the error is reported to the user.
- I understand why the insert()/update()/delete() methods were created, but I don't think ultimately they'll be helpful. They're muddying the waters of where SQL is generated. For example, when using your update() method, you'll need to write a bit of SQL for the WHERE clause, but everything else is generated by create_update_sql_string (which should be called with $this-> in front of it anyway). I think it would be much cleaner to have the SQL query built in one place, so you don't have multiple points of failure, and multiple chunks of code that are suspect when something goes wrong.

Re: First try at OOPHP

Posted: Tue Jul 14, 2009 5:03 pm
by Korken
Thanks you so much!
So much I didn't know and so much to learn. :D
__set() and __get() helped a lot!

I wonder though why I never have heard of MySQLi.
It does everything I wanted my class to do in an OOP way, so mine feels a bit unnecessary.

//Emil

Re: First try at OOPHP

Posted: Tue Jul 14, 2009 5:27 pm
by Christopher
Korken wrote:Hmm, I'm not quite sure what you mean. Could you give an example?

Code: Select all

<?php
class Db_Adapter_Mysql
{   
    private $host;
    private $db;
    private $username;
    private $password;
    
    private $db_link;
    private $db_result;
    
    public function __construct($array)
    {
        if (isset($array['host']) || isset($array['db']) || isset($array['username']) || isset($array['password']))
            exit("Error: There are invalid connection properties.");
        
        $this->host = $array['host'];
        $this->db = $array['db'];
        $this->username = $array['username'];
        $this->password = $array['password'];
    }
    
    public function connect()
    {
        $this->db_link = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
        mysql_select_db($this->db) or die(mysql_error());
        
        return $this->db_link;
    }
    
    public function disconnect()
    {
        mysql_close($this->db_link);    
    }
    
    public function query($db_sql)
    {
        $this->db_result = mysql_query($db_sql) or die(mysql_error());
        return new Db_Adapter_Mysql_Result($this->db_result);
    }
    
    public function numRows($table)
    {
        $row = mysql_fetch_array($this->query("SELECT COUNT(*) AS numrows FROM $table"), MYSQL_ASSOC);
        return (int)$row['numrows'];
    }
}
 
class Db_Adapter_Mysql_Result
{   
    protected $db_result;
 
    public function __construct($db_result)
    {
        $this->db_result =$db_result;
    }
    
    public function fetchRow($result = null)
    {
        if (!$result)
             $result = $this->db_result;
             
        return mysql_fetch_row($result);
    }
    
    public function fetchArray($result = null)
    {
        if (!$result)
             $result = $this->db_result;
             
        return mysql_fetch_array($result);
    }
    
    public function escapeString($string)
    {
        if(get_magic_quotes_gpc())
            $string = stripslashes($string);
            
        return mysql_real_escape_string($string);
    }
 
}    
 
class Db_TableDateGateway
{   
    protected $db;
    protected $table;
 
    public function __construct($db, $table)
    {
        $this->db =$db;
        $this->table = $table;
    }
    
    public function insert($array)
    {
        $this->db->query(create_insert_sql_string($this->table, $array));
    }
    
    public function update($array, $where)
    {
        $this->db->query(create_update_sql_string($this->table, $array, $where));
    }
    
    public function delete($where)
    {
        $this->db->query("DELETE FROM {$this->table} WHERE $where");
    }
    
    private function create_insert_sql_string($table, $array)
    {
        $place = "";
        $values = "";
        
        foreach ($array as $key=>$val)
        {
            $place .= "$key, ";
            
            if (strtolower($val) == "null")
                $values .= "NULL, ";
            elseif (strtolower($val) == "now()")
                $values .= "NOW(), ";
            else
                $values .= "'" . $val . "', ";
        }
        
        $place = substr_replace($place, "", -2);
        $values = substr_replace($values, "", -2);
        return "INSERT INTO $table($place) VALUES($values)";
    }
    
    private function create_update_sql_string($table, $array, $where)
    {
        $str = "";
        
        foreach ($array as $key=>$val)
        {
            $str .= "$key = ";
            
            if (strtolower($val) == "null")
                $str .= "NULL, ";
            elseif (strtolower($val) == "now()")
                $str .= "NOW(), ";
            else
                $str .= "'" . $val . "', "; 
        }
        
        $str = substr_replace($str, "", -2);
        return "UPDATE $table SET $str WHERE $where";
    }
}
?>

Re: First try at OOPHP

Posted: Fri Jul 17, 2009 7:47 am
by Korken
Ahh, now I understand! Thanks!

//Emil

Re: First try at OOPHP

Posted: Sat Jul 18, 2009 1:44 am
by Benjamin
:offtopic:

I think there are 4 missing ! and two missing braces.

Code: Select all

 
        if (isset($array['host']) || isset($array['db']) || isset($array['username']) || isset($array['password']))
            exit("Error: There are invalid connection properties.");
 

Re: First try at OOPHP

Posted: Sat Jul 18, 2009 6:58 am
by Korken
Yes indeed, I saw them some time ago but haven't updated the code here, sorry. :|

//Emil

Edit: The latest version (still under testing):

Code: Select all

<?php
/**********************************************************************
 * - Made in the Eclipse IDE: http://www.eclipse.org
 * - Tested on PHP 5.2
 * 
 * Usage of the class MySQLAdapter
 * 
 * Attention!: "word"/'word' are strings, without the " or the ' are ints and anything with 'array' in its name is an array.
 *             Every function returns false if there was a problem, use getError() to se the error.
 * 
 * -> On creation of the object:
 * You must specify the connection array.
 * Ex: $db_con = new MySQLAdapter(array('host' => 'localhost',
 *                                      'username' => 'my_username',
 *                                      'password' => 'my_pass',
 *                                      'db' => 'my_database'));
 * 
 * -> getError():
 * This will return the latest error.
 * 
 * -> connect():
 * This will connect to the database.
 * Don't forget to close the connection when you are done.
 * 
 * -> disconnect():
 * This will close the connection to the database.
 * 
 * -> escapeString("string"):
 * This will return the string ready for insert into/update a database.
 * 
 * -> numRows("table"):
 * This will return the number of row in a table.
 * 
 * -> query("SQL string");
 * This will send a SQL string to the database and return the result.
 * 
 * -> fetchRow("result"):
 * This will return a mysql_fetch_row from "result".
 * If "result" is unspecified it will return from the last Query.
 * 
 * -> fetchArray("result"):
 * This will return a mysql_fetch_array from "result".
 * If "result" is unspecified it will return from the last Query.
 * 
 * -> insert("table", data_array):
 * This will insert data into the database.
 * Ex: $db_con->insert("users", array('NICK' => 'my_nick',
 *                                    'ADMIN' => '1',
 *                                    'PASSWORD' => 'my_pass'));
 *                                    
 * SQL string produced: "INSERT INTO users(NICK, ADMIN, PASSWORD) VALUES('my_nick', '1', 'my_pass')" 
 * 
 * Attention: NOW() and NULL works.
 * Ex: $db_con->insert("users", array('NICK' => 'my_nick',
 *                                    'ADMIN' => 'NULL',
 *                                    'PASSWORD' => 'my_pass',
 *                                    'DATE' => 'NOW()'));
 *                                    
 * SQL string produced: "INSERT INTO users(NICK, ADMIN, PASSWORD, DATE) VALUES('my_nick', NULL, 'my_pass', NOW())"
 * 
 * 
 * -> update("table", data_array, "where"):
 * This will update the database.
 * The string "where" is where you want to do the update, ex: ID = 2
 * Ex: $db_con->update("users", array('NICK' => 'my_new_nick',
 *                                    'PASSWORD' => 'my_new_pass'), "ID = 12 LIMIT 1");
 *                                    
 * SQL string produced: "UPDATE users SET NICK = 'my_new_nick', PASSWORD = 'my_new_pass' WHERE ID = 12 LIMIT 1"
 * Attention: NOW() and NULL works.
 *
 * 
 * -> delete("table", "where"):
 * This will delete a post from a database.
 * Ex: $db_con->delete("users", "ID = 7");
 * 
 * SQL string produced: "DELETE FROM users WHERE ID = 7"
 * 
 * 
 * 
 * Connection array design:
 *  __________________________
 * |     Key    |    Value    |
 * |  (string)  |   (string)  |
 * |------------|-------------|
 * |    host    |   my_host   |
 * |- - - - - - | - - - - - - |
 * |     db     |     my_db   |
 * |- - - - - - | - - - - - - |
 * |  username  | my_username |
 * |- - - - - - | - - - - - - |
 * |  password  | my_password |
 *  --------------------------
 *  
 * Insert and Update array design (example):
 *  __________________________
 * |  TableCol  |    Value    |
 * |  (string)  |   (string)  |
 * |------------|-------------|
 * |    NICK    |   my_nick   |
 * |- - - - - - | - - - - - - |
 * |   ADMIN    |      1      |
 * |- - - - - - | - - - - - - |
 * |  PASSWORD  | my_password |
 *  --------------------------
 **********************************************************************/
 
class MySQLAdapter
{   
    private $db_link;
    private $db_result;
    private $db_error = null;
    
    public function __construct($array)
    {
        if (!isset($array['host']) || !isset($array['username']) || !isset($array['password']) || !isset($array['db']))
        {       
            $this->db_error = "The host, username, password and/or db are not set.";
            return false;
        }
        
        $this->db_link = mysql_connect($array['host'], $array['username'], $array['password']);
        if (!$this->db_link)
        {
            $this->db_error = mysql_error();
            return false;
        }
            
        if (!mysql_select_db($array['db'], $this->db_link))
        {
            $this->db_error = mysql_error();
            return false;
        }
        
        return $this->db_link;
    }
    
    public function disconnect()
    {
        mysql_close($this->db_link);    
    }
    
    public function query($db_sql)
    {
        $this->db_result = mysql_query($db_sql, $this->db_link);
 
        if (!$this->db_result)
        {
            $this->db_error = mysql_error();
            return false;
        }
 
        return $this->db_result;
    }
    
    public function insert($table, $array)
    {
        return $this->query($this->create_insert_sql_string($table, $array));
    }
    
    public function update($table, $array, $where)
    {
        return $this->query($this->create_update_sql_string($table, $array, $where));
    }
    
    public function delete($table, $where)
    {
        return $this->query("DELETE FROM $table WHERE $where");
    }
    
    public function fetchRow($result = null)
    {
        if (!$result)
             $result = $this->db_result;
             
        return mysql_fetch_row($result);
    }
    
    public function fetchArray($result = null)
    {
        if (!$result)
             $result = $this->db_result;
             
        return mysql_fetch_array($result);
    }
    
    public function escapeString($string)
    {
        if(get_magic_quotes_gpc())
            $string = stripslashes($string);
 
        return mysql_real_escape_string($string);
    }
 
    public function numRows($table)
    {
        $row = mysql_fetch_array($this->query("SELECT COUNT(*) AS numrows FROM $table"), MYSQL_ASSOC);
        return (int)$row['numrows'];
    }
    
    public function getError()
    {
        return $this->db_error;
    }
    
    private function create_insert_sql_string($table, $array)
    {
        $place = "";
        $values = "";
        
        foreach ($array as $key=>$val)
        {
            $place .= "$key, ";
            
            if (strtolower($val) == "null")
                $values .= "NULL, ";
            elseif (strtolower($val) == "now()")
                $values .= "NOW(), ";
            else
                $values .= "'" . $val . "', ";
        }
        
        $place = substr_replace($place, "", -2);
        $values = substr_replace($values, "", -2);
        return "INSERT INTO $table($place) VALUES($values)";
    }
    
    private function create_update_sql_string($table, $array, $where)
    {
        $str = "";
        
        foreach ($array as $key=>$val)
        {
            $str .= "$key = ";
            
            if (strtolower($val) == "null")
                $str .= "NULL, ";
            elseif (strtolower($val) == "now()")
                $str .= "NOW(), ";
            else
                $str .= "'" . $val . "', "; 
        }
        
        $str = substr_replace($str, "", -2);
        return "UPDATE $table SET $str WHERE $where";
    }
}
?>

Re: First try at OOPHP

Posted: Sun Aug 16, 2009 1:33 am
by Dalar_ca
arborint wrote:It is certainly a start, although your class are just Value Objects. In PHP they are little better than arrays. Also, camelCase() is the PHP standard.
Color me a bit lost here - when did PHP declare a naming 'standard' for objects/classes/etc? Seems quite the opposite to me, anyways, as PHP doesn't have a single camel case function built into it that I'm aware of.

Re: First try at OOPHP

Posted: Sun Aug 16, 2009 2:26 am
by Eran

Re: First try at OOPHP

Posted: Sun Aug 16, 2009 12:28 pm
by Christopher
Dalar_ca wrote:Color me a bit lost here - when did PHP declare a naming 'standard' for objects/classes/etc? Seems quite the opposite to me, anyways, as PHP doesn't have a single camel case function built into it that I'm aware of.
How about these two built-in and widely used class libraries? I think they would be a good examples of what is considered the current standard in PHP:

SPL: http://us2.php.net/manual/en/book.spl.php
PDO: http://us2.php.net/manual/en/book.pdo.php

There are many others. Certainly there are examples of other styles both inside and outside of the PHP Group (especially PHP4 era code), but camel case is certainly the current standard for PHP classes. See also, popular PHP5 frameworks like Zend, Cake, etc. You might not like it, or be comfortable with it yet -- but it's the standard.