kael.shipman Posted July 2, 2007 Share Posted July 2, 2007 Hey, I have a class whose functions all use the same global variables (a specific mysql connection), but when I tried to make the global $sql_connection local to the class, it threw errors: <?php class cha { global $sql_connection; //Error here: "Parse error: syntax error, unexpected T_GLOBAL, expecting T_FUNCTION" function fah() { @mysql_query("insert something",$sql_connection); } function lah() { @mysql_query("select something",$sql_connection); } //a few more functions } ?> I assume this means that I can't use "global" in the context of a class, but is there something comparable or do I have to localize the $sql_connection variable within each individual function? Thanks, Kael Link to comment https://forums.phpfreaks.com/topic/58078-solved-global-variables-within-classes/ Share on other sites More sharing options...
no_one Posted July 2, 2007 Share Posted July 2, 2007 You could probably try something like, not sure, and haven't tested it. <?php class blah { var $sqlc; function __construct() { global $sql_connection; $this->sqlc = &$sql_connection; } } ?> Link to comment https://forums.phpfreaks.com/topic/58078-solved-global-variables-within-classes/#findComment-287965 Share on other sites More sharing options...
ToonMariner Posted July 2, 2007 Share Posted July 2, 2007 there is no global contect in classes. you need to pass the $sql_connection to the class by assigining it in the script creating the class like so... <?php $clss_ref = new cha; $clss_ref->dbconn = $sql_connection; ?> then in the class itself you can then use $this->db_conn; OR if you only have the one just leave it out of the mysql_query functio - php automatically uses the last connection it created if no resource is passed in the argument... Link to comment https://forums.phpfreaks.com/topic/58078-solved-global-variables-within-classes/#findComment-287966 Share on other sites More sharing options...
kael.shipman Posted July 2, 2007 Author Share Posted July 2, 2007 Cool; Thanks guys. Link to comment https://forums.phpfreaks.com/topic/58078-solved-global-variables-within-classes/#findComment-287979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.