php class constant call $this::CONST - php

Why I can call class constant from dynamic declaration?
This code works well:
echo $this::CONST;
Isn't it wrong?

From http://php.net/manual/en/language.oop5.constants.php
<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "\n";
}
}
echo MyClass::CONSTANT . "\n";
$classname = "MyClass";
echo $classname::CONSTANT . "\n"; // As of PHP 5.3.0
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."\n"; // As of PHP 5.3.0
?>
Calling a constant from a dynamic declaration works just as well as calling it from the class

Related

how to select this class in php?

php:
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "\n";
}
}
echo MyClass::CONSTANT . "\n"; //this way selects MyClass.
My question is that could I select this class as following:
echo $this->CONSTANT . "\n"; // I think it's not right way, how could I do?
I want to select current class.
I'm very new to php and while learning php this question came into my mind. So forgive me If I'm asking nonsense question.
CONST properties are accessible without the need of instantiating the class.
CONST are also shared across all the instances of the class in memory.
To access a CONST:
ClassName::CONST
or given a class referenced in a variable:
$myclassvar::CONST
Or in class Context:
self::CONST
This is well explained here:
http://www.php.net/manual/en/language.oop5.constants.php
<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "\n";
}
}
echo MyClass::CONSTANT . "\n";
$classname = "MyClass";
echo $classname::CONSTANT . "\n"; // As of PHP 5.3.0
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."\n"; // As of PHP 5.3.0
?>
You can do this:
<?php
$a = new MyClass();
echo $a::CONSTANT;
?>

PHP __FILE__ inheritence

I am trying to write a method that I can use in extended classes that uses the file's full path. However, when I use __FILE__ I only get back the path for the class the inherited method is defined in. Here is an example:
foo.php
class foo
{
public function getFullFile()
{
return __FILE__;
}
}
bar.php
class bar extends foo
{
public function getFile()
{
return __FILE__;
}
}
example.php
$foo = new foo();
echo $foo->getFullFile() . PHP_EOL;
$bar = new bar();
echo $bar->getFullFile() . PHP_EOL;
echo $bar->getFile() . PHP_EOL;
The output of running example.php:
/my/full/path/foo.php
/my/full/path/foo.php
/my/full/path/bar.php
Is this expected behavior? Is there a better way to accomplish this? Am I doing something insanely stupid?
You can not refer to __FILE__ (for said reasons), but you can refer to the file the current object's class has been defined in:
class foo
{
public function getFullFile()
{
$c = new ReflectionClass($this);
return $c->getFileName();
}
}
__FILE__ is always the file where the constant is referred to. It's for debugging purposes to you can easily refer back to the exact file where the code that uses __FILE__ is located. It would be useless to say that it was in 'bar.php', when the definition resides in 'foo.php'.
Some code says more than thousand words...
B.php
<?
class B {
public function bar() {
return __FILE__;
}
}
A.php
<?
require "B.php";
class A extends B {
public function foo() {
echo parent::bar();
}
}
$a = new A();
$a->foo();
joe#joe-desktop:~$ php A.php
/home/joe/B.php
See http://php.net/manual/en/language.constants.predefined.php for more information on these 'magical constants'. If I am not mistaken these magical constants are a sort of macros and are determined at compile time, not runtime.
This is the expected behaviour __FILE__ will always evaluate to the filename of file in which interpreter finds it. Same with other magic constants (__METHOD__, __CLASS__, __LINE__ etc)
As far as I can tell, it is impossible to do waht you're trying to do.
I think if you want $bar->getFullFile() to return bar.php, you'll have to add the function to bar.php.
public function getFullFile() {
return parent::getFullFile();
}
Just use debug_backtrace() PHP function. Example of use (my library function my_die is placed in some included php file, but reports the callers location!):
function my_die($msg)
{
$bt = debug_backtrace();
$file = $bt[0]["file"];
$line = $bt[0]["line"];
$func = $bt[1]["function"];
# session info also!!!
$user = $_SESSION['user'];
error_log('Died in ' . $file . ': ' . $line . ': function ' . $func .
"(): user='{$user}': ip='{$_SERVER['REMOTE_ADDR']}': " . $msg .
" (browser: '{$_SERVER['HTTP_USER_AGENT']})'");
die($msg);
}

output constructor variable value in oop php

My concept is very poor in oop for php. My class has a constructor with three parameters.i create a object and pass three values to the constructor. Now, how will i show constructor value.
class Foo {
public function __constructor($para1, $para2, $para3 ){
echo $para1 . '<br>';
echo $para2 . '<br>';
echo $para3 . '<br>';
}
}
$f = Foo(10,20,30);

How do I get the function name inside a function in PHP?

Is it possible?
function test()
{
echo "function name is test";
}
The accurate way is to use the __FUNCTION__ predefined magic constant.
Example:
class Test {
function MethodA(){
echo __FUNCTION__;
}
}
Result: MethodA.
You can use the magic constants __METHOD__ (includes the class name) or __FUNCTION__ (just function name) depending on if it's a method or a function... =)
If you are using PHP 5 you can try this:
function a() {
$trace = debug_backtrace();
echo $trace[0]["function"];
}
<?php
class Test {
function MethodA(){
echo __FUNCTION__ ;
}
}
$test = new Test;
echo $test->MethodA();
?>
Result: "MethodA";

Accessing a CONST attribute of series of Classes

This is how I wanted to do it which would work in PHP 5.3.0+
<?php
class MyClass
{
const CONSTANT = 'Const var';
}
$classname = 'MyClass';
echo $classname::CONSTANT; // As of PHP 5.3.0
?>
But I'm restricted to using PHP 5.2.6. Can anyone think of a simple way to simulate this behavior without instantiating the class?
You can accomplish this without using eval in pre-5.3 code. Just use the constant function:
<?php
class MyClass
{
const CONSTANT = 'Const var';
}
$classname = 'MyClass';
echo constant("$classname::CONSTANT");
?>
If you absolutly need to access a constant like that, you can do this:
<?php
class MyClass
{
const CONSTANT = 'Const var';
}
$classname = 'MyClass';
echo eval( 'return '.$classname.'::CONSTANT;' );
?>
But, if i were you, I'd try not to use eval.

Categories