jeeva Posted February 19, 2007 Share Posted February 19, 2007 hi frnds, can i declare the php function as a constant like define("CONNECT","odbc_connect");. if i used like that than it will gives the error message.i don't know we can use or should not use here my code define("CONNECT","odbc_connect"); class db { var $dsn="dbconnect"; //dsn var $uname=""; var $password=""; var $con=NULL; //db connect function DbConnect() { $this->con=CONNECT($this->dsn,$this->uname,$this->password) or die(odbc_errormsg()); return $this->con; } } it's gives the following error Fatal error: Call to undefined function: connect() in c:\inetpub\wwwroot\access\class.php on line 22 wts up? Quote Link to comment https://forums.phpfreaks.com/topic/39124-can-i-use-define-function-like-this/ Share on other sites More sharing options...
Monkeymatt Posted February 19, 2007 Share Posted February 19, 2007 PHP is a scripting language and does not go through and replace constants first like pre-processor directives. To do such a thing, you would have to do it this way: $CONNECT="odbc_connect"; ... $this->con=$CONNECT($this->dsn,$this->uname,$this->password) or die(odbc_errormsg()); Quote Link to comment https://forums.phpfreaks.com/topic/39124-can-i-use-define-function-like-this/#findComment-188429 Share on other sites More sharing options...
jeeva Posted February 19, 2007 Author Share Posted February 19, 2007 thanks Monkeymatt But if i used like that its gives the following error Parse error: parse error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in c:\inetpub\wwwroot\access\class.php on line 18 Quote Link to comment https://forums.phpfreaks.com/topic/39124-can-i-use-define-function-like-this/#findComment-188431 Share on other sites More sharing options...
wildteen88 Posted February 19, 2007 Share Posted February 19, 2007 Add curly braces around the constant eg: define("CONNECT", "odb_connect"); $con = {CONNECT}('bla', 'blah, 'bla'); Now PHP should replace {CONNECT} with the constants value. Quote Link to comment https://forums.phpfreaks.com/topic/39124-can-i-use-define-function-like-this/#findComment-188910 Share on other sites More sharing options...
jeeva Posted February 20, 2007 Author Share Posted February 20, 2007 No ...........its still keep telling error.. better to drop this concept i think . Quote Link to comment https://forums.phpfreaks.com/topic/39124-can-i-use-define-function-like-this/#findComment-189352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.