magic-constants

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

拈花ヽ惹草 提交于 2019-12-21 09:15:10
问题 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 __DIR__.'/../../../../web/'.$this->getUploadDir(); } Code directly coming from official documentation (How to handle file uploads with Doctrine) But SLInsight raises a warning if the code analysed contains __DIR__ or __FILE__ PHP magic constants: __DIR__ and __FILE__

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

a 夏天 提交于 2019-12-12 03:55:41
问题 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 Debug($Message,$File=__FILE__,$Line=__LINE__) { ... } The problem I am running in to is that if I define the above method in a file other than the one I am 'debugging', I end up with the file and line from the file the method is defined in, rather than the one I am

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

不羁岁月 提交于 2019-12-07 13:48:42
问题 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 and would expect it to be somewhere in the new PHP 5.5, I don't see a way to doing it. So here it is: Say we have some class, that uses some trait that conflicts with some function inside the class, example: class SomeClass { use \Name\Space

Path of current PHP script relative to document root

倾然丶 夕夏残阳落幕 提交于 2019-12-06 23:11:24
问题 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 include this file from multiple (different) directories. My directory structure looks somewhat like this: www ├── (getpath.php) │ ├── css │ └── style.css ├── php │ └── header.php └── content ├── index.php └── posts └── index.php (I'm including header

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

蹲街弑〆低调 提交于 2019-12-06 03:40:35
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 and would expect it to be somewhere in the new PHP 5.5, I don't see a way to doing it. So here it is: Say we have some class, that uses some trait that conflicts with some function inside the class, example: class SomeClass { use \Name\Space\SomeTrait { SomeFunction as private NewFunctionName; } function SomeFunction() { $this->NewFunctionName(); } }

PHP get line number from logging event

主宰稳场 提交于 2019-12-05 23:25:23
问题 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 = new Logger(); $log->debug('hello'); // Say this is line #20 Now in my Logger.php class in the debug() I use the __Line __ Magic Constant on for example line #300. When I run the script I would like the log entry to read 'on line 20' but it read

Path of current PHP script relative to document root

匆匆过客 提交于 2019-12-05 02:50:40
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 include this file from multiple (different) directories. My directory structure looks somewhat like this: www ├── (getpath.php) │ ├── css │ └── style.css ├── php │ └── header.php └── content ├── index.php └── posts └── index.php (I'm including header.php in both index.php files. header.php is the file that refers to style.css (which the browser needs to

PHP get line number from logging event

自古美人都是妖i 提交于 2019-12-04 06:19:13
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 = new Logger(); $log->debug('hello'); // Say this is line #20 Now in my Logger.php class in the debug() I use the __Line __ Magic Constant on for example line #300. When I run the script I would like the log entry to read 'on line 20' but it read 'on line 300'. Besides passing the line number to the function is there any other way I could do this?

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

时光毁灭记忆、已成空白 提交于 2019-12-04 03:08:51
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 __DIR__.'/../../../../web/'.$this->getUploadDir(); } Code directly coming from official documentation ( How to handle file uploads with Doctrine ) But SLInsight raises a warning if the code analysed contains __DIR__ or __FILE__ PHP magic constants: __DIR__ and __FILE__ constants may conflict with the Symfony resource overriding system. How usage of this constants can

Get calling file name from include()

允我心安 提交于 2019-11-27 21:36:58
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 to do this? Or is it impossible due to the way PHP is interpreted? This is actually just a special case of what PHP templating engines do. Consider having this function: function ScopedInclude($file, $params = array()) { extract($params); include $file; } Then A.php can include C.php like this: <?php // A.php ScopedInclude('C.php', array(