Jump to content

Dynamic links in an include file


KnottyAlder

Recommended Posts

I am working on a small piece of code that counts records from a database and displays the result to the user. Everything works fine, except I am running into difficulties with dynamic links.

 

The code that accesses my database is implemented inside a class. It works fine except that when I directly include the database include file, the script cannot recognize that the database class exists. I have to dynamically link it. But the page that is running the counting script (and needs to include the database file) is also included on other pages.

 

Any ideas? Did my weak explanation make sense?

Link to comment
https://forums.phpfreaks.com/topic/177621-dynamic-links-in-an-include-file/
Share on other sites

The explanation was hard to follow, I have an idea of what might be wrong though, let me know if I have it right:

 

Are you defining a connection to a database and then you are unable to access that connection inside of class methods?

 

If this is the case, make sure you are defining the database in a global scope (that is, outside of any functions or classes - or if it is in a function or class, the database is being assigned to a variable that has been initialized in that function or class using the global keyword).

 

If you are, start your class method by recognizing the database as a global variable:

 

<?php
function some_class_method() {
global $db;
/* Rest of code */
}
?>

 

Does that help?

If this is the case, make sure you are defining the database in a global scope (that is, outside of any functions or classes - or if it is in a function or class, the database is being assigned to a variable that has been initialized in that function or class using the global keyword).

 

Defining variables as global is terrible advice. Globals are considered bad practice in many programming languages including PHP.

If this is the case, make sure you are defining the database in a global scope (that is, outside of any functions or classes - or if it is in a function or class, the database is being assigned to a variable that has been initialized in that function or class using the global keyword).

 

Defining variables as global is terrible advice. Globals are considered bad practice in many programming languages including PHP.

 

I think that's highly debatable. When dealing with MySQL connections, far better to define 1 connection and use it consistently through a script then make several connections to the MySQL database (quite a bit of wasted over head and can even lead to scripts failing due to too many connections).

 

If you know of any significant security issues addressed by passing a database connection to functions/methods that are not addressed by using a global variable to refer to that connection please let me know, might be worth updating some of my PHP if this is the case!

 

I use classes to handle database interactions, webpage interactions, permissions, validations etc. and let methods and functions refer to these objects using the global keyword as they need. With proper use of privacy I see no security that could be breached that couldn't be breached with other methods.

If you know of any significant security issues addressed by passing a database connection to functions/methods that are not addressed by using a global variable to refer to that connection please let me know, might be worth updating some of my PHP if this is the case!

 

Global variables can be overwritten. So...

 

function dofoo() {
  global $db;
  // use $db
}

 

Simply assumes that $db is a database connection. There is of course no guarantee it is.

 

on the other hand....

 

function dofoo(My_Db $db) {
  // use $db;
}

 

We know that $db at least implements the My_Db object.

Thanks, but that is not exactly what I meant.

 

Here's what I'm trying to do:

 

On my website I have a right side bar that is included on every page. It will be in a file that sits in the main directly (/signUp.php), and it will be in a file that sits in a subdirectory (/portal/messages.php). The right side bar has a series of links to other pages on the site, but I have hard linked them, so there are no problems. The links works whether I include it in signUp.php (in the main directory) or in messages.php (in a subdirectory).

 

However, yesterday I coded a short script to count items from the database (user messages to be precise), and that number is displayed on the right side bar. It works fine except that because, as mentioned above, the right side bar is included on a number of pages (many of which are in subdirectories), I have to hard link the page that connects to my database. The file is included, but I get an error that says no such class (the page has a database class) was found. The page must be dynamically linked, but I have to hard link it to insure that it will work on all pages regardless of what subdirectory the right side bar is included on.

 

Any ideas?

 

(I hope that made more sense.)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.