Jump to content

dunno

New Members
  • Content Count

    6
  • Joined

  • Last visited

Community Reputation

0 Neutral

About dunno

  • Rank
    Newbie
  1. @mac_gyver. Thank you for your answer, short and to the point. If I understand you well, the fact that I use the property is enough: there is no scope issues for a $this var within a method; certainly that would explain why the program works with $db uncommented at a global level. I am not sure I understand what you mean by I mean, why use it if it doesn't impact the functionality of the program at all? Do you mean from the human readability perspective? or, does it impact the code in some way, it becomes somehow "more" visible? Forgive my ignorance but I'd really like to get to the
  2. Thank you for your reply. Here is a snapshot of the working version of the Class Connection: note the private $db is commented out just for the test: Class Connection{ //private $db; //var global to class, so that both functions may use it public function __construct(){ try{ //define database details $data = DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";"; //new PDO object: database details, username, password $this->db = new PDO($data,DB_USER,DB_PASS); show('CONNECTED!'); //show($db); //$db is now a new PDO object }catch(PDOException $e){
  3. Thank you very much for your detailed answer; I've been trying to digest it over the weekend, and it has clarified some of my foundational wrong conceptions about PHP scope. I think every PHP beginner should read this post! Firstly, I thought that a global variable and a local variable of the same name were the same; but as you show in example 1, the opposite is true. Secondly, the apparently simple truth that that for a beginner is a tricky concept, is an important piece of wisdom. There is just one point I don't fully understand; in my pagination code, if I comment out the
  4. Hi Requinix, Thank you very much for your excellent answer. To be honest the expression global $db = new PDO($data,DB_USER,DB_PASS); was me in a desperate attempt to make the property visible to row_count(). But your solution makes total sense: the global scope within the class must be inside the class but outside any function, and the call to that property within a function must be accessed using the "$this" keyword. A simple error which I will not forget. Thank you for pointing it out. Have a great weekend! Sept
  5. Hi Requinix, Sorry about my inexperience, but I know now for the future. Thank you for your fast response. This is the Connection class Class Connection{ public function __construct(){ try{ //define database details $data = DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";"; //new PDO object: database details, username, password global $db = new PDO($data,DB_USER,DB_PASS); show('CONNECTED!'); //show($db); //$db is now a new PDO object }catch(PDOException $e){ die($e->getMessage()); } }//close constructor public function
  6. Hi guys, I am trying to implement a Pagination class for a website displaying movies. I need to have the total number of posts, but on accessing the function row_count() from class Connection I get the error: "Fatal error: Uncaught Error: Call to a member function query() on null in ..\connection.php:30 Stack trace: #0 from ...\index.php(20): Connection->row_count() #1 ..." I only found two posts concerning this issue, and they led me to think this might be a scope issue with $db variable; $db is defined in the constructor function of the same Connection class so I thought the sco
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.