I have a static class Foo (this isn't a real class, so static fields are just for example)
class Foo{
public static $name = "foo";
public static $age = "18";
public static $city = "Boston";
}
In my code I want to build an array of all the public static properties and their current values.
Is there a quick/easy way anyone can suggest to do this without instantiating a Foo?
Use a ReflectionClass instance like this to get an array of the property names and values:
$class = new ReflectionClass('Foo');
$staticProperties = $class->getStaticProperties();
foreach ($staticProperties as $propertyName => $value) {
// Your code here
}
Use the Reflection
<?php
require_once "Foo.php";
$reflection = new ReflectionClass("Foo");
$staticProperties = $reflection->getStaticProperties();
print_r($staticProperties)
http://php.net/manual/en/reflectionclass.getstaticproperties.php
http://ca2.php.net/manual/en/reflectionclass.getstaticpropertyvalue.php
Related
I want to create an object from inside the method of an object of the same class in PHP.
I have this simple code:
<?php
class Animal {
public $name = "cat";
public function test() {
$this->name = "dog";
return new self;
}
}
$animal = new Animal;
$best = $animal->test();
echo $animal->name;
echo '<br>';
echo $best->name;
?>
The code works just fine with the new object getting assigned to the $best variable. However, I keep reading that self keyword is used in static context only (for static methods, properties, and constants).
Is what I'm doing correct or I should refer to the class name in another way other than self in this case?
I'm trying to write a class that uses its parent's static declared array to add a new value to. Below is kind of a feel of how I'm trying to run it...
class SuperClass
{
protected static $array_name = array('value1');
}
class SubClass extends SuperClass
{
protected static $array_name = array_push(parent::$array_name, 'value2');
}
Is there any way to properly implement this without a __construct() function?
I'm trying to implement a static working model for the SuperClass and its parents...
I'm not entirely sure if you want static classes completely, but this does what you want give or take :
<?php
class SuperClass
{
static $array_name = array('value1');
}
class SubClass extends SuperClass
{
static $array_name = 'foo';
function __construct(){
self::$array_name = array_push(parent::$array_name, 'value2');
}
}
$foo = new SubClass();
var_dump($foo::$array_name); // prints INT 2 - as push returns number of elements in array.
?>
I have two classes. Class B has field: object of class A (composition relationship). It is necessary to get static variable of class A. But there are some problems in code:
<?php
class A {
public static $var = 'a';
}
class B {
private $object;
private function staticAccess($className) {
$this->object = $className;
}
public function __construct() {
$this->staticAccess('A');
// This is wrong syntax:
//$a = $this->object::$var;
// Syntax which works but unconvenient
$objA = $this->object;
$a = $objA::$var;
}
}
As you saw there is a solution. But it is necessary to write additional line. Is it possible to solve a task in one line?
Thanks you for any help!
It's not possible to do in one line (just a constraint of PHP). I'd suggest adding a function that you can use, something like this:
public function getStaticVar($var) {
$class = new ReflectionClass($this->object);
$value = $class->getStaticPropertyValue($var);
return $value;
}
Using the Reflection library is the only way of dynamically accessing a dynamic static property in PHP.
I have a string containing the name of a class. This class is abstract, but has a public static method returning an instance of a child class.
abstract class MyClass {
public static function instance() {
return self::$inst;
}
}
Now I need to call this method somehow and all I am given is the name of the class as a string. I can't say $class = new $className() because MyClass is abstract. Any ideas?
If you have the classname in a string and want to call an abstract method of that class, you could do the following:
$className = 'MyClass';
$instance = $className::instance();
I finally found a solution for this - Reflection.
$refClass = new ReflectionClass('MyClass');
if ($refClass->hasMethod('instance') {
$refMethod = new ReflectionMethod('MyClass', 'instance');
$refMethod->invoke(null);
}
I know I am late, but if somebody is still looking, do the following:
$method = "myFunction";
$class = "myClass";
$result = $class::$method();
So in the mentioned case use
$method = "instance";
$class = "myClass"
$instance = $class::$method();
But in your case the problem seems to be in your instance function. I guess you try to return an instance of your abstract class, which is not possible !
i want to call a class method by a var (like this):
$var = "read";
$params = array(...); //some parameter
if(/* MyClass has the static method $var */)
{
echo MyClass::$var($params);
}
elseif (/* MyClass hat a non-static method $var */)
{
$cl = new MyClass($params);
echo $cl->$var();
}
else throw new Exception();
i read in the php-manual how to get the function-members of a class (get_class_methods). but i get always every member without information if its static or not.
how can i determine a method´s context?
thank you for your help
Use the class ReflectionClass:
On Codepad.org: http://codepad.org/VEi5erFw
<?php
class MyClass
{
public function func1(){}
public static function func2(){}
}
$reflection = new ReflectionClass('MyClass');
var_dump( $reflection->getMethods(ReflectionMethod::IS_STATIC) );
This will output all static functions.
Or if you want to determine whether a given function is static you can use the ReflectionMethod class:
On Codepad.org: http://codepad.org/2YXE7NJb
<?php
class MyClass
{
public function func1(){}
public static function func2(){}
}
$reflection = new ReflectionClass('MyClass');
$func1 = $reflection->getMethod('func1');
$func2 = $reflection->getMethod('func2');
var_dump($func1->isStatic());
var_dump($func2->isStatic());
One way I know of is to use Reflection. In particular, one would use ReflectionClass::getMethods as such:
$class = new ReflectionClass("MyClass");
$staticmethods = $class->getMethods(ReflectionMethod::IS_STATIC);
print_r($staticmethods);
The difficulty of this is that you need to have Reflection enabled, which it is not by default.