ΠŸΡ€Π΅Π΄ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½Π½Ρ‹Π΅ константы

Для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ скрипта, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ запускаСт PHP, прСдусмотрСли Π½Π°Π±ΠΎΡ€ ΠΏΡ€Π΅Π΄ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½Π½Ρ‹Ρ… констант. ΠŸΡ€ΠΈ этом Ρ‡Π°ΡΡ‚ΡŒ констант этого списка ΡΠΎΠ·Π΄Π°ΡŽΡ‚ ΠΌΠΎΠ΄ΡƒΠ»ΠΈ. ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ Ρ‚Π°ΠΊΠΈΠ΅ константы доступны Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΏΡ€ΠΈ доступности ΠΌΠΎΠ΄ΡƒΠ»Π΅ΠΉ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π»ΠΈΠ±ΠΎ динамичСски Π·Π°Π³Ρ€ΡƒΠ·ΠΈΠ»ΠΈ, Π»ΠΈΠ±ΠΎ Π²ΠΊΠ»ΡŽΡ‡ΠΈΠ»ΠΈ Π² сборку PHP ΠΏΡ€ΠΈ компиляции.

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
292
vijaykoul_007 at rediffmail dot com ΒΆ
20 years ago
the difference between 
__FUNCTION__ and __METHOD__ as in PHP 5.0.4 is that

__FUNCTION__ returns only the name of the function

while as __METHOD__ returns the name of the class alongwith the name of the function

class trick
{
      function doit()
      {
                echo __FUNCTION__;
      }
      function doitagain()
      {
                echo __METHOD__;
      }
}
$obj=new trick();
$obj->doit();
output will be ----  doit
$obj->doitagain();
output will be ----- trick::doitagain
up
46
Tomek Perlak [tomekperlak at tlen pl] ΒΆ
19 years ago
The __CLASS__ magic constant nicely complements the get_class() function.

Sometimes you need to know both:
- name of the inherited class
- name of the class actually executed

Here's an example that shows the possible solution:

<?php

class base_class
{
    function say_a()
    {
        echo "'a' - said the " . __CLASS__ . "<br/>";
    }

    function say_b()
    {
        echo "'b' - said the " . get_class($this) . "<br/>";
    }

}

class derived_class extends base_class
{
    function say_a()
    {
        parent::say_a();
        echo "'a' - said the " . __CLASS__ . "<br/>";
    }

    function say_b()
    {
        parent::say_b();
        echo "'b' - said the " . get_class($this) . "<br/>";
    }
}

$obj_b = new derived_class();

$obj_b->say_a();
echo "<br/>";
$obj_b->say_b();

?>

The output should look roughly like this:

'a' - said the base_class
'a' - said the derived_class

'b' - said the derived_class
'b' - said the derived_class
up
12
php at kenman dot net ΒΆ
12 years ago
Just learned an interesting tidbit regarding __FILE__ and the newer __DIR__ with respect to code run from a network share: the constants will return the *share* path when executed from the context of the share.

Examples:

// normal context
// called as "php -f c:\test.php"
 __DIR__ === 'c:\';
__FILE__ === 'c:\test.php';

// network share context
// called as "php -f \\computerName\c$\test.php"
 __DIR__ === '\\computerName\c$';
__FILE__ === '\\computerName\c$\test.php';

NOTE: realpath('.') always seems to return an actual filesystem path regardless of the execution context.
up
8
Sbastien Fauvel ΒΆ
10 years ago
Note a small inconsistency when using __CLASS__ and __METHOD__ in traits (stand php 7.0.4): While __CLASS__ is working as advertized and returns dynamically the name of the class the trait is being used in, __METHOD__ will actually prepend the trait name instead of the class name!
up
0
public at taliesinnuin dot net ΒΆ
5 years ago
If you're using PHP with fpm (common in this day and age), be aware that __DIR__ and __FILE__ will return values based on the fpm root which MAY differ from its actual location on the file system.

This can cause temporary head-scratching if deploying an app where php files within the web root pull in PHP files from outside of itself (a very common case). You may be wondering why __DIR__ returns "/" when the file itself lives in /var/www/html or whathaveyou.

You might handle such a situation by having NGINX explicitly add the necessary part of the path in its fastcgi request and then you can set the root on the FPM process / server / container to be something other than the webroot (so long as no other way it could become publicly accessible).

Hope that saves someone five minutes who's moving code to FPM that uses __DIR__.