how to select this class in php? - 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;
?>

Related

php class constant call $this::CONST

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

Using thread in php, ununderstood behaviour

here is a simple example of what I am not understanding :
<?php
class classA {
private $z = 1;
public function __construct() {
$this->b = new classB;
$this->b->setRefonClassA($this);
$this->b->start();
}
function changeZ() {
echo "ChangeZ : z=" . $this->z . "\n";
$this->z = 666;
echo "ChangeZ : z=" . $this->z . "\n";
}
function showZ() {
echo "showZ : z=" . $this->z . "\n";
}
}
class classB extends Thread {
function setRefOnClassA($classA) {
$this->classA = $classA;
}
function run() {
$this->classA->changeZ();
$this->classA->showZ();
}
}
$test = new classA();
?>
The result are :
ChangeZ : z=1
ChangeZ : z=666
showZ : z=1
I was expecting that showZ=666. Why z=1 ?
I'm clearly missing something here.
Thanks in advance
Can you try extending classA from Stackable?
There is a similar problem:
pthread Thread objects reset their state
Read this: https://gist.github.com/krakjoe/6437782
You have two problems, first classA is not thread safe, if you don't descend from pthreads it is serialized when set as a member of classB. If classA does descend from pthreads, when you write it to classB you loose the real reference and pthreads will not allow you to access the same functionality - it thinks you are in another thread.
The key is, "You are responsible for the objects you create", this means, if you intend to share an object among threads, you must retain a direct reference to it in the scope that created the object.

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);

PHP Object Question

Unfortunately I cannot provide any code examples, however I will try and create an example.
My question is about Objects and memory allocation in PHP.
If I have an object, lets say:
$object = new Class();
Then I do something like
$object2 = $object;
What is this actualy doing? I know there is a clone function, but thats not what I'm asking about, I'm concerned about whether this is creating another identical object, or if its just assigning a reference to $object.
I strongly understand this to mean that it just creates a reference, but in some case usages of mine, I find that I get another $object created, and I can't understand why.
If you use the magic method __invoke, you can call an object similar to a function, and it will call that magic method.
class Object{
function __invoke(){ return "hi"; }
}
$object = new Object;
$object2 = $object();
echo $object2; // echos hi
That means that $object2 is equal to whatever that function returns.
Basically, you are calling a function, but using a variable as it's name. So:
function test(){ echo "hi"; }
$function_name = "test";
$function_name(); // echos hi.
In this case, you are just calling an object instead.
So, in reference to your question, this is actually not 'cloning' at all, unless the __invoke() function looks like this:
function __invoke(){ return this }
In which case, it would be a reference to the same class.
You are creating a second reference of the same object. Here is a proof:
<?php
class TestClass {
private $number;
function __construct($num) { $this->number = $num; }
function increment() { $this->number++; }
function __toString() { return (string) $this->number; }
}
$original = new TestClass(10);
echo "Testing =\n";
echo "--------------------------------\n";
echo '$equal = $original;' . "\n";
$equal = $original;
echo '$equal = ' . $equal . ";\n";
echo '$original->increment();' . "\n";
$original->increment();
echo '$equal = ' . $equal . ";\n";
echo "\n";
echo "Testing clone\n";
echo "--------------------------------\n";
echo '$clone = clone $original;' . "\n";
$clone = clone $original;
echo '$clone = ' . $clone . ";\n";
echo '$original->increment();' . "\n";
$original->increment();
echo '$clone = ' . $clone . ";\n";
Use clone if you want to create a copy of an instance.
Assuming that you mean
$object2 = $object;
And not
$object2 = $object();
PHP will create a reference to the original object, it will not copy it. See http://www.php.net/manual/en/language.oop5.basic.php, the section called
Object Assignment.
<?php
class Object{
public $value = 1;
public function inc(){
$this->value++;
}
}
$object = new Object;
$object2 = $object;
$object->inc();
echo $object2->value; // echos 2, proving it's by reference

Categories