Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 899705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:15:50+00:00 2026-05-15T15:15:50+00:00

If I have a class (e.g Database) can I check the location of the

  • 0

If I have a class (e.g Database) can I check the location of the script that tried to instantiate this class.

An example:

I have an example dir structure like so:

lib
  Database.class.php
app
  action.class.php

and in action.class.php:

$db = new Database;

and in Database.class.php

function __construct(){
$name = //some code to find out that app/action.class.php just tried to instantiate me
$name = explode('/',$name);
if($name[0] = 'app') //allow script to use the class
else $this->__destruct; //dont allow other scripts not in app dir for example
}

I dont know if this is possible or not

Regards
Luke

UPDATE: It seems like this is possible but not good practice (from answers below) thank you for your time, I wont be implimenting this mainly due to @Gordons argument.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:15:50+00:00Added an answer on May 15, 2026 at 3:15 pm

    If the calling instance or it’s location in the filesystem is a dependency in the Database class, then pass it in through the constructor. If you need find out to where the file is located, use Reflection or the magic __FILE__ constant, e.g.

    // action.class.php 
    public function initDb()
    {
        $this->db = new Database($this);
    }
    
    // Database.php
    public function __construct($invoker)
    {
        // reflective way to get $invokerLocation
        $reflector = new ReflectionClass($invoker);
        $invokerLocation = $reflector->getFileName();
        // check location
    }
    

    or simply

    // action.class.php 
    public function initDb()
    {
        $this->db = new Database(__FILE__);
    }
    
    // Database.php
    public function __construct($invokerLocation)
    {
        // check location
    }
    

    You do not want debug_backtrace to find out what called the Database constructor. Like the name implies, it is for debugging purposes. Depending on the size of the trace stack, fetching the backtrace can consume a lot of time and resources. Passing it is cleaner and faster.

    In addition, if you make security of your code depend on where files are located, then you introduce a dependency on the filesystem, which is code smell to me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that inserts information into a MySQL database. I have the
I have a class that saves logging information to the database (in an NServiceBus
I have a class with like 20 fields which get populated from SQL database
I have a PHP script that logs informational and error messages to a database
I have a form that has this code in it, so I can echo
I have been working on this form for my php class. I can not
I have a class that handles data. Other classes can give it their values
I have a class which defines a historical extraction on a database: class ExtractionConfiguration
I have a database class, which manages all my mysql connections for my application.
i have these in Database.php class Database { private $dbname = 'auth'; private $db_instance;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.