Questions tagged [magic-constants]

PHP provides a number of 'magic constants' such as __LINE__, __FILE__, __FUNCTION__, __CLASS__ and __METHOD__. Use this tag with questions tagged [php] that ask about these, and any other, magic constants.

If your question is about PHP (so it has a tag) and is also intrinsically about one of the magic constants in PHP, then you can use this tag.

Examples of such magic constants include:

  • __LINE__
  • __FILE__
  • __FUNCTION__
  • __CLASS__
  • __METHOD__
  • __DIR__

Etc.

31 questions
28
votes
3 answers

Xdebug weird __DIR__ constant

I am writing an PHP CLI app which loads YAML files. When trying to do this within an Xdebug session: if (file_exists(__DIR__ . '/../../foo/bar') { /* ... */ } __DIR__ allways is xdebug: which will allways lead to false from file_exists(). Is…
Jan P.
  • 3,261
  • 19
  • 26
20
votes
2 answers

Python equivalent of PHP's __DIR__ magic constant?

In PHP, the __DIR__ magic constant evaluates to the path to the directory containing the file in which that constant appears. Is there an equivalent feature in Python?
user212218
12
votes
5 answers

Get calling file name from include()

I want to get the name of the file that includes another file from inside the included file. I know there is the __FILE__ magic constant, but that doesn't help, since it returns the name of the included file, not the including one. Is there any way…
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
10
votes
4 answers

Path of current PHP script relative to document root

TL;DR: What alternatives to the last code sample on this page are there when trying to use relative links on pages included with PHP's include command? I'm trying to include() a file that links to a .css document, the problem is that I need to…
9
votes
3 answers

Why absolute path constants __DIR__ and __FILE__ should not be used in Symfony

I use SensioLabs Insight to control my code quality. For a simple file upload, I have to get the absolute path of my uploads directory: protected function getUploadRootDir() { // the absolute directory path where uploaded return…
chalasr
  • 12,971
  • 4
  • 40
  • 82
7
votes
2 answers

PHP get line number from logging event

Ok I have another question HERE for my Logging Class but I wanted to be able to add the line number of the calling script to the log file entry. I have seen __Line__ but this gives me the line number of the line where this is at. Example: a.php $log…
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
6
votes
3 answers

Best practices for usage of PHP’s Magic Constants

What are the best practices for usage of PHP’s Magic Constants, such as __LINE__ , __FILE__, __FUNCTION__, __CLASS__ and __METHOD__? For example I use __LINE__ and __CLASS__ to locate the SQL Error like this : $result = mysql_query($query) or…
Amirouche Douda
  • 1,564
  • 1
  • 21
  • 30
3
votes
2 answers

Anyone know of a PHP Magic Constant for Trait's Redefined Name in a Class?

To make it simple, I have noticed that PHP doesn't seem to offer any magic constant for determining what the name that a trait has been changed to in a class. Since this sounds confusing to me in words, I will give an example, as it is rather easy…
NinjaKC
  • 821
  • 9
  • 15
2
votes
1 answer

LUA indexed table access via named constants

I am using LUA as embedded language on a µC project, so the ressources are limited. To save some cycles and memory I do always only indexed based table access (table[1]) instead og hash-based access (table.someMeaning = 1). This saves a lot of…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
2
votes
1 answer

Can I change current directory to parent directory using PHP __DIR__ constant?

I am using PHP magic constant to move uploaded file to a new location, and the upload handler file location is inside "_inc" file inside the main application folder like this: "path/tasksapp/_inc/upload_handelr.php" So; when I use __DIR__ I get…
CairoCoder
  • 3,091
  • 11
  • 46
  • 68
2
votes
3 answers

How to write or open a php array without evaluating or expanding magic constant __DIR__

I have a config file that is a php array named config.php. return array( 'template_dir' => __DIR__ '/configs/templates.php' ) Then whenever I want to use this config file I would just include config.php. Its also really easy to write a config…
lumberjacked
  • 966
  • 1
  • 21
  • 35
1
vote
1 answer

PHP - It is possible to get the magic constant __FILE__ of the requirer script inside the required script?

It is possible to get the magic constant __FILE__ of a requirer script( the file that requires another ) inside the required file? For example: requirer.php
1
vote
0 answers

Is there a magic constant in PHP like __DIR__ but with unresolved symlinks?

Everything is in the title. I am trying to write an app in php with client and server features. For testing purpose (at least), I need several clients. To not have to copy the written code, I thought using symbolic links. Something like this : My…
Tuckbros
  • 417
  • 3
  • 13
1
vote
1 answer

What are the security risks of using PHP magic constants?

PHP's magic constants allow you access information such as the current PHP file being executed, the name of the current function, etc. While this data is not user input directly, it can definitely be influenced by user input (eg: by visiting a…
jamieweb
  • 123
  • 4
1
vote
2 answers

PHP get grandfather dir path

I know PHP > 5.3 added the Magic constants : __DIR__ to get the father dir path, But how can I get the grandfather dir path in a smarter way? 1.__DIR__.'/../' 2.dirname(__DIR__)
fire790620
  • 49
  • 8
1
2 3