AndyPSV Posted May 16, 2014 Share Posted May 16, 2014 (edited) Hello,maybe back to basics, but pobierz, plaintext $db = new PDO('mysql:host=localhost;dbname=andypsv_cocain','root','',array(PDO::ATTR_PERSISTENT=>true)); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); define('PRFX','drug_'); class PAGE { var $_t,$_k,$_d,$_c,$_nav,$_,$tpl,$u,$md; function PAGE($md='',$md2='',$md3='',$md4='') { $q = $db->query('SELECT * FROM `'.PRFX.'c0'.LG.'`'); if($q->rowCount() == 0) die(_E.'no categories'); while($_c0 = $q->fetch()) $c0[] = $_c0; $this->tpl->assign('c0',$c0); ... PRZY CZYM... INNE KLASY. class HELP extends PAGE { function i() { // I want to use here $db->query How to handle this issue to use class $db inside other classes?Up to this point I've used solely functions so it worked smoothly (i.e. mysql_query() alias).How? Edited May 16, 2014 by AndyPSV Quote Link to comment https://forums.phpfreaks.com/topic/288531-classes/ Share on other sites More sharing options...
AndyPSV Posted May 16, 2014 Author Share Posted May 16, 2014 I've added function PAGE($md='',$md2='',$md3='',$md4='') { global $db; and it works... Quote Link to comment https://forums.phpfreaks.com/topic/288531-classes/#findComment-1479664 Share on other sites More sharing options...
Jacques1 Posted May 16, 2014 Share Posted May 16, 2014 Did you run the code through some kind of obfuscator? This is the most incomprehensible thing I've seen in a while. Quote Link to comment https://forums.phpfreaks.com/topic/288531-classes/#findComment-1479672 Share on other sites More sharing options...
bsmither Posted May 16, 2014 Share Posted May 16, 2014 Using global $db is one way. Another way I see often (and confused me because I didn't realize what was going on) is to realize that variables assigned in the global scope can be reached via the $GLOBALS['db'] super-variable. You may want to initially use $GLOBALS['db'] = new PDO(). Quote Link to comment https://forums.phpfreaks.com/topic/288531-classes/#findComment-1479692 Share on other sites More sharing options...
ginerjm Posted May 16, 2014 Share Posted May 16, 2014 Why are you playing with classes and inheritance at this stage in your PHP development? You have so many problems and apparently don't recognize proper syntax yet. Just stick to writing some decent code and grow into classes later on, otherwise you will only be re-writing them again and again as you FINALLY begin to catch on. 1 - where is $db defined? 2 - you set your pdo to retrieve assoc arrays but then you try and retrieve the data with numeric indices. 3 - you fetch your entire result set and store it into an array to then pass that array on to some method. Why? Much smarter functions available to you but you haven't realized that yet. Read the manual on 'fetchall' Quote Link to comment https://forums.phpfreaks.com/topic/288531-classes/#findComment-1479693 Share on other sites More sharing options...
TOA Posted May 16, 2014 Share Posted May 16, 2014 Global will work, but is not advised. Inject the dependency. Again, using globals is not advised. Quote Link to comment https://forums.phpfreaks.com/topic/288531-classes/#findComment-1479695 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.