Re: constant from variable
| From: | Nathan Rixham | Date: | Fri, 04 Jun 2010 22:11:23 +0000 |
| Subject: | Re: constant from variable | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-305841@lists.php.net to get a copy of this message | ||
Tanel Tammik wrote:
"Nathan Rixham" <nrixham@gmail.com> wrote in message news:4C097083.3080803@gmail.com...You're half way to building a DAO pattern / an ORM, been down this road several times myself and recognise it well. <snip-code looks cool, no feedback - sure you'll figure it out as you hit things over the coming days and weeks ;)Tanel Tammik wrote:i'm using also somekind of patter... don't have a name for it!""Tanel Tammik"" <keevitaja@gmail.com> wrote in message news:31.A3.00596.0D7590C4@pb1.pair.com...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"Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote in message news:1275678975.2217.83.camel@localhost...this is better: <?php define('PERFIX', 'dbperfix_'); define('DB_MYTABLE', PERFIX . 'mytable'); abstract class Database { private static function table_name() {On Fri, 2010-06-04 at 22:07 +0300, Tanel Tammik wrote:<?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 TanelHi, define('MYCONST', 'something'); $my = 'my'; $const = 'const'; is it possible to get the value of MYCONST using variables $my and $const_ Br TanelI 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.ukreturn 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(); ?>
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!But best of all, you're playing, learning, hacking and using the latest versions of your tools, so all credit to you and hope you have fun finding your own way - it's a wonderful thing when you read a book written by a coding legend in years to come and recognise what they are saying, and moreover have came to the same conclusions yourself (or even better you can disagree and back up your reasons why). Good luck & keep going, you're doing great. Nathan