"Nathan Rixham" <nrixham@xxxxxxxxx> wrote in message news:4C097083.3080803@xxxxxxxxxxxx > Tanel Tammik wrote: >> ""Tanel Tammik"" <keevitaja@xxxxxxxxx> wrote in message >> news:31.A3.00596.0D7590C4@xxxxxxxxxxxxxxx >>> "Ashley Sheridan" <ash@xxxxxxxxxxxxxxxxxxxx> wrote in message >>> news:1275678975.2217.83.camel@xxxxxxxxxxxx >>>> On Fri, 2010-06-04 at 22:07 +0300, Tanel Tammik wrote: >>>> >>>>> Hi, >>>>> >>>>> define('MYCONST', 'something'); >>>>> >>>>> $my = 'my'; >>>>> $const = 'const'; >>>>> >>>>> >>>>> is it possible to get the value of MYCONST using variables $my and >>>>> $const_ >>>>> >>>>> Br >>>>> Tanel >>>>> >>>>> >>>>> >>>> >>>> I don't really see how you can? The only correlation at all is that the >>>> two variables use the same letters combined as their values as the name >>>> of the constant but in a different case. Variables and constants in PHP >>>> are case-sensitive. >>>> >>>> Why are you trying to do this anyway? Perhaps there's a better way than >>>> what you are trying to do. >>>> >>>> Thanks, >>>> Ash >>>> http://www.ashleysheridan.co.uk >>>> >>>> >>>> >>> <?php >>> define('PERFIX', 'dbperfix_'); >>> define('DB_MYTABLE', PERFIX . 'mytable'); >>> >>> abstract class Database { >>> private static function table_name() { >>> $table_name = strtolower(get_called_class()); >>> return constant('DB_' . strtoupper($table_name)); >>> } >>> >>> public static function return_query() { >>> return "select * from " . static::table_name() . " where >>> something='value'"; >>> } >>> } >>> >>> class MyTable extends Database { >>> } >>> >>> echo MyTable::return_query(); >>> ?> >>> >>> i know i could just do return PERFIX . $table_name. - i'm using it that >>> way. i was just curious if the other way is possible. might come handy >>> some day! >>> >>> Br >>> Tanel >>> >> >> this is better: >> >> <?php >> define('PERFIX', 'dbperfix_'); >> define('DB_MYTABLE', PERFIX . 'mytable'); >> >> abstract class Database { >> private static function table_name() { >> return constant('DB_' . strtoupper(get_called_class())); >> } >> >> public static function return_query() { >> return "select * from " . static::table_name() . " where >> something='value'"; >> } >> } >> >> class MyTable extends Database { >> } >> >> echo MyTable::return_query(); >> ?> > > I'm jumping a few steps here, but you may be interested in looking at this > pattern :) > > http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html > > i'm using also somekind of patter... don't have a name for it! <?php abstract class DatabaseObject { public static function table_name() { $table_name = strtoupper(end(explode('\\', get_called_class()))); return constant('DB_' . $table_name); } public static function find_by_sql($query) { global $db; $rows = array(); $result = $db->query($query); while($row = $db->fetch_assoc($result)) { $rows[] = static::init($row); } return $rows; } public static function find_by_id($id = 0) { $result_array = static::find_by_sql("select * from " . static::table_name() . " where id='" . (int)$id . "' limit 1"); return (!empty($result_array)) ? array_shift($result_array) : false; } public static function find_all($order_by = 'id') { $result_array = static::find_by_sql("select * from " . static::table_name() . " order by " . $order_by); return $result_array; } protected static function table_fields() { global $db; $result = $db->query("show columns from " . static::table_name()); while($row = $db->fetch_array($result)) { $table_fields[] = array_shift($row); } return $table_fields; } protected static function init($row) { global $db; $obj = new static; foreach($row as $field => $value) { $obj->$field = $value; } return $obj; } protected function attributes() { global $db; $attributes = array(); foreach(static::table_fields() as $field) { if(property_exists($this, $field)) { $attributes[$field] = $db->escape_value($this->$field); } } return $attributes; } public function save() { return isset($this->id) ? $this->update() : $this->create(); } public function create() { global $db; $attributes = $this->attributes(); $query = "insert into " . static::table_name() . " ("; $query .= join(', ', array_keys($attributes)); $query .= ") values('"; $query .= join("', '", array_values($attributes)); $query .= "')"; if($db->query($query)) { return $db->insert_id(); } } public function update() { global $db; $attributes = $this->attributes(); foreach($attributes as $key => $value) { $attribute_pairs[] = "{$key} = '$value'"; } $query = "update " . static::table_name() . " set "; $query .= join(', ', $attribute_pairs); $query .= " where id='" . (int)$this->id . "'"; if($db->query($query)) { return $this->id; } } public function delete() { global $db; $query = "delete from " . static::table_name() . " where id='" .(int)$this->id. "'"; if($db->query($query)) { return true; } } } class MyTable extends DatabaseObject { } $myrow = MyTable::find_by_id($id); $myrow->name = $name; $myrow->save(); $newrow = new MyTable; $newrow->name = $name; $newrow->save(); PHP 5.3 is fun! With namespaced i can autoload all classes in different folders as well using namespace corresponding to the path to the class file! Br Tanel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php