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
1
vote
2 answers

Referring to a PHP superclass name via a magic constant / keyword or similar construct (avoiding naming the superclass in the derived class)

The comments in the following code show what I am trying to accomplish, which is something very simple: I want to be able to refer to the name of the parent class using a PHP built-in constant (or other construct) such as __CLASS__, but which refers…
John Sonderson
  • 3,238
  • 6
  • 31
  • 45
1
vote
1 answer

Dart: Magic Constant Equivalents

PHP offers useful magic constants like: __CLASS__ __FILE__ __METHOD__ and so on. Also the get_class() function provides a similar functionality. Is there anything similar in Dart?
wellnoidea
  • 193
  • 10
0
votes
1 answer

Is there a way in php to count the nested stucture level?

I'm experimenting with a way to more easily debug Cronjob Scripts with PHP. without having xdebug etc. accessible on the server. For this, I'd like to get the number of nested structures the code entered. I'll call it NESTING with in the…
Philipp
  • 137
  • 1
  • 2
  • 11
0
votes
2 answers

How to get the name of the script calling a function?

I want to write a very generic function for logging : function msglog($type, $message) { echo date("r") . " " . __FILE__ . " - $type - $message" . PHP_EOL; } I am going to use it almost everywhere in my code which will be dispatched in several php…
Tuckbros
  • 417
  • 3
  • 13
0
votes
1 answer

PHP magic constants - automatically passed to function?

I wrote a simple access control system which reads an array of access strings, and return true or false depending on the result. I would call it as follows (for example in a method list_user_data of class User): ` if (current_user_can(__CLASS__,…
Kobus Myburgh
  • 1,114
  • 1
  • 17
  • 46
0
votes
1 answer

Can magic constants be imported in PHP

I cannot find any hint about the fact I cannot import any magic constant. Trying to like ...
codekandis
  • 712
  • 1
  • 11
  • 22
0
votes
1 answer

Using magic constants to access object properties

I am about to load a language object as a property from controller object like this: $this->lang = new Languages(en) Inside the methods of my controller object i am currently accessing the specific translation like this: function =…
user11694442
0
votes
1 answer

Magic Constants from called file - not action file

I have this function in my class: logMagic($mode) { # mode # 1 = all, 2 = dir, 3 = file etc. # this is wrapped inside a switch statement # for eases sake here's the case 1: code $log['dir'] = 'DIRECTORY: '. __DIR__; …
treyBake
  • 6,440
  • 6
  • 26
  • 57
0
votes
1 answer

How to define constants in php and access it from all files without including the constants file (any thing like AppSettings in .Net)

I want define some constants and access those from any file without including any file in php. in the other word i search for any equivalent of Web.Config->AppSettings in C#
Keyvan
  • 3
  • 1
  • 1
0
votes
1 answer

Downsides to using __DIR__

I recently came across the magic constant __DIR__ when resolving an issue regarding including scripts within included scripts. After doing some reading I couldn't find any reason not to use __DIR__ to prefix every path in a require or include…
gnusey
  • 354
  • 3
  • 16
0
votes
1 answer

What does mean by "symlinks resolved" in the description of magic constant __FILE__?

I'm using PHP 7.2.0 Below is a description of magic constant __FILE__ : The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned. I didn't understand meaning of the…
user9059272
0
votes
1 answer

Can anyone tell what is the relation of include and require with magic constants in php?

I am learning to include and require constructs on php.net and found the line An exception to this rule are magic constants which are evaluated by the parser before the include occurs. on 7 paragraph of that page but didn't understand that what is…
user8480171
0
votes
1 answer

Is there any way to carry down a PHP magic constant as a function default?

The idea here is to create a method for logging and debugging purposes, that doesn't require passing said method the associated 'magic constants'. Effectively, I'm trying to achieve this using a method definition like so: function…
DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41
0
votes
2 answers

Unable to compare magic constant __METHOD__

Ok, I think I have something here... Inside a class, im trying to condition a private function based on the used method's name. So the code looks something like:
Solrac
  • 924
  • 8
  • 23
0
votes
0 answers

defining __DIR__ in PHP class

Is there a reason why this would cause a 500 error? Something to do with when it's executed and what's available, or am I using __DIR__ incorrectly? class MyClass { public $filepath = __DIR__ . "/../downloads"; }
d-_-b
  • 21,536
  • 40
  • 150
  • 256