Can I use PHP's \ReflectionClass without explicitly setting the class name?
My aim is to get class information inside its own context.
Instead of:
class Test {
public function getSomeClassInfo() {
$reflection = new \ReflectionClass('Test');
// ...
}
}
I want to do:
class Test {
public function getSomeClassInfo() {
$reflection = new \ReflectionClass({$this});
// ...
}
}
As the documentation of PHPs ReflextionClass shows the class constructor accepts two alternate arguments: either a class name or an object. Since $this is a pointer to an object I'd say that should be possible. If not (give it a test!), then just use the generic class name constant __CLASS__ instead.
Related
Some code first...
FlashBagUtil class containing the constants:
class FlashBagUtil
{
const TYPE_NOTICE = 'notice';
const TYPE_WARNING = 'warning';
const TYPE_ALERT = 'alert';
const LANG_EN = 'en';
const LANG_RU = 'ru';
const LANG_IL = 'il';
}
Parent class:
class CoreController
{
public $flashUtil;
public function __construct()
{
$this->flashUtil = new FlashBagUtil;
}
}
Child class:
class BatchController extends CoreController
{
public function indexAction()
{
// Method 1 - This works fine
$flash = $this->flashUtil;
$flashType = $flash::TYPE_NOTICE;
// Method 2 - This, obviously, does not
$flashType = $this->flashUtil::TYPE_NOTICE;
// Method 3 - Neither does this as $flashUtil is a non-static instantiated object
$flashType = self::$flashUtil::TYPE_NOTICE;
}
}
PHP documentation states: A property declared as static cannot be accessed with an instantiated class object (though a static method can).
But I seem to be able to do that with the first method. What am I missing?
+
Is Method 1 the only and cleanest way of accessing static content in this context?
You're referencing a class constant, which is different than a class variable (property) and is accessible to instantiated objects. The documentation you're referencing refers to class variables defined with the static keyword (ie. private static $flashUtil;), which may be the source of your confusion if you're accustomed to programming in other more strictly typed OOP languages.
if you want to use your class as enum, make the enumerationsclass abstract:
abstract FlashBagUtil
{
const TYPE_NOTICE = 'notice';
...
}
and use it in your childclass:
class Controller
{
private flashType = FlashBagUtil::TYPE_NOTICE;
}
Making it an abstract class like suggested won't be much of help here, I think, as there are more things going on in FlashBagUtil class that I've removed for the example code.
My Method 1 works but requires making a copy of the original object which defies the purpose of a common inherited object. So...
In the end I've settled on a standard way to access static content directly by importing the namespace to child class and use $flashType = FlashBagUtil::TYPE_NOTICE as suggested by Ralphael. Would've been nice to access the constants from the object in a one liner, but this keeps static content nicely separated as well.
Full child class:
use TreasureForge\CoreBundle\Util\FlashBagUtil;
class BatchController extends CoreController
{
public function indexAction()
{
$flash = FlashBagUtil::TYPE_NOTICE;
}
}
Many thanks for your input.
This might be basic knowledge, but I am curious as I do not know it yet myself. Why in PHP (and assuredly other languages) when using classes must a child class use a construct method to access the parent class' properties. In case this is unclear I will include an example.
<?php
class aClass
{
protected $aProperty = "Some value";
}
class aDifferentClass extends aClass
{
public $aDifferentProperty;
public function __construct()
{
$this->$aDifferentProperty = $this->aProperty;
}
?>//Works.
Instead of:
<?php
class aClass
{
protected $aProperty = "Some value";
}
class aDifferentClass extends aClass
{
public $aDifferentProperty = $this->$aProperty;
}
?>//Doesn't work.
Its not a matter of needing the constructor its a matter of WHEN you're trying to access it. A class is a blueprint for an object -- when you're trying to assign the property, as you've done in your example above, ie.
public $aDifferentProperty = $this->aProperty;
There is no object, and thus "this" does not yet exist. But, instead, this would work:
class A {
protected $a_property = "BOOYEA!";
}
class B extends A {
public function show_me_a_prop() {
echo $this->a_property;
}
}
$object = new B();
$object->show_me_a_prop();
So, to answer your question, you must wait until after the object has been constructed to access the properties because until its constructed, its not an object, just a blueprint for an object.
Now, to take this a bit further, you'd not allowed to assign variables directly to properties (see http://php.net/manual/en/language.oop5.properties.php ) but you can assign a constant. So here's a similar example which does work:
class A {
const a_property = "BOOYEA!";
}
class B extends A {
public $b_property = self::a_property;
}
$object = new B();
echo $object->b_property;
The "__construct" was introduced in PHP5 and it is the right way to define your constructors (in PHP4 you used the name of the class for a constructor).
You are NOT REQUIRED to define a constructor in your class, but if you wish to pass any parameters on object construction THEN YOU NEED ONE.
Also...If further down the track you change the class the child class inherits from, you don't have to change the construct call to the parent.
much easier to call parent::__construct() instead of parent::ClassName(), as it is reusable among classes and the parent can be changed easily.
Is there any difference in constructor or unified constructor. I have a class which have a constructor and a unified constructor. When i intialise object of the class then unified constructor call why not normal constructor.
<?php
class test {
function __construct() {
print "In BaseClass constructor\n";
}
function test() {
echo 'Class loeaded';
}
}
$obj = new test();
?>
OUTPUI
In BaseClass constructor
PHP Version 5.2.6
Since PHP 5, the best way to declare a constructor method in a class is to use the standardized
__construct()
So if you have
class myA {
public function __construct() {
echo "hello construct myA";
}
public function myA() {
echo "hello basic way myA";
}
}
$myA = new myA();
It will echo
hello construct myA
because the __constructor() has priority
But, for compatibility reason, the old way may work if there is not __construct() method.
And:
class myA {
//public function __construct() {
// echo "hello construct A";
//}
public function myA() {
echo "hello basic way myA";
}
}
$myA = new myA();
will give you:
hello basic way myA
In all cases, I advise you to use __construct()
the old constructor (method with class name) is only there for compatibility reasons. so if you have the new constructor (__construct) in the class php won't bother to call the old one.
edit
interesting note is that calling parent::__construct when the class being extended has only the old constructor type will still work (it becomes like a alias to the real constructor)
Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Read documentation of constructor (link)
Instances of classes are created using the new keyword. What happens during the new call is that a new object is allocated with its own copies of the properties defined in the class you requested, and then the constructor of the object is called in case one was defined. The constructor is a method named __construct(), which is automatically called by the new keyword after creating the object. It is usually used to automatically perform various initializations such as property initializations. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor the function parameters in between the parentheses.
In PHP 4, instead of using __construct() as the constructor’s name, you had to define a method with the classes’ names, like C++. This still works with PHP 5, but you should use the new unified constructor naming convention for new applications.
We could pass the names of the people on the new line in the following class example :
class Person {
function __construct($name)
{
$this->name = $name;
}
function getName()
{
return $this->name;
}
private $name;
};
$judy = new Person("Judy") . "\n";
$joe = new Person("Joe") . "\n";
print $judy->getName();
print $joe->getName();
Tip: Because a constructor cannot return a value, the most common practice for raising an error from within the constructor is by throwing an exception.
According to documentation As of PHP 5.3.3, methods with the same name as that of class will no longer be treated as constructor .. They are like regular methods ...
<?php
class myclass
{
public function myclass()
{
// treated as constructor in PHP 5.3.0-5.3.2
// treated as regular method as of PHP 5.3.3
}
}
?>
`
For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, and the class did not inherit one from a parent class, it will search for the old-style constructor function, by the name of the class.
It means that there if you have constructor myclass() and __construct .. then __construct will be searched for first and taken as constructor instead of myclass()
class myclass
{
public function myclass()
{
echo 'hello';
}
function __construct() {
print "new constructor"
}
}
$obj = new myclass(); // will echo new constructor
Let's suppose I have a parent class in PHP like this:
class A {
private $property;
public static function factory($arg) {
$object = new A();
$object->property = $arg;
return $object;
}
}
And I want to extend it in this way:
class B extends A {
public static function factory() {
return parent::factory('constant');
}
}
When I do B::factory() I get a object of type A. What if I want an object of type B? I cannot change anything in the code of class A.
1st version
That's because you hardcoded the A class in the factory method.
In class A, instead of $object = new A() try (require Php 5.3):
$class_name = get_called_class();
$object = new $class_name;
get_called_class() "Gets the name of the class the static method is called in."
Shorter version:
$object = new static();
2nd version (hardcoded parent class):
Copy object properties manually:
$parent = parent::factory($args);
$obj = new static();
$obj->setTimestamp($parent->getTimestamp());
$obj->setTimezone($parent->getTimezone());
return $obj;
Or use an hack to do it autoatically:
How to Cast Objects in PHP
In your example:
You have two classes (unleast)
both classes can be instantiated (concrete, not pure or abstract)
one class is a superclass of another
both classes are instantiated with a "factory" method
the "factory" method of a subclass can invoke the the "factory" method of the superclass
each "factory" method can have several type or count of parameters
Problem
Now, this is what it got my attention:
class B extends A {
public static function factory() {
return parent::factory('constant');
}
}
Short & Quick answer:
Change that to:
class B extends A {
public static function factory() {
return A::factory('constant');
}
}
Long boring hipster extended answer:
You are attempting to overriding ( and overloading, with different parameters ) a static function.
Its a common mistake, that assume that static methods are virtual, and can be overrided. Some programming languages allow that (Object Pascal, Delphi), other don't (C#, Java), PHP depends on the version, I think.
To be honest, "static functions" work similar like global functions with a namespace, that have public access to all members of a class, Instead of methods. I suggest to see them as global functions, always.
Cheers.
I understand that there are two ways to access a PHP class - "::" and "->". Sometime one seems to work for me, while the other doesn't, and I don't understand why.
What are the benefits of each, and what is the right situation to use either?
Simply put, :: is for class-level properties, and -> is for object-level properties.
If the property belongs to the class, use ::
If the property belongs to an instance of the class, use ->
class Tester
{
public $foo;
const BLAH;
public static function bar(){}
}
$t = new Tester;
$t->foo;
Tester::bar();
Tester::BLAH;
The "::" symbol is for accessing methods / properties of an object that have been declared with the static keyword, "->" is for accessing the methods / properties of an object that represent instance methods / properties.
Php can be confusing in this regard you should read this.
What's also confusing is that you can call non static functions with the :: symbol. This is very strange when you come from Java. And it certainly surprised me when I first saw it.
For example:
class Car
{
public $name = "Herbie <br/>";
public function drive()
{
echo "driving <br/>";
}
public static function gas()
{
echo "pedal to the metal<br/>";
}
}
Car::drive(); //will work
Car::gas(); //will work
$car = new Car();
$car->drive(); // will work
$car->gas(); //will work
echo $car->name; // will work
echo Car::$name; // wont work error
As you can see static is very loose in php. And you can call any function with both the -> and the :: symbols. But there is a difference when you call with :: there is no $this reference to an instance. See example #1 in the manual.
When you declare a class, it is by default 'static'. You can access any method in that class using the :: operator, and in any scope. This means if I create a lib class, I can access it wherever I want and it doesn't need to be globaled:
class lib
{
static function foo()
{
echo "hi";
}
}
lib::foo(); // prints hi
Now, when you create an instance of this class by using the new keyword, you use -> to access methods and values, because you are referring to that specific instance of the class. You can think of -> as inside of. (Note, you must remove the static keyword) IE:
class lib
{
function foo()
{
echo "hi";
}
}
$class = new lib;
$class->foo(); // I am accessing the foo() method INSIDE of the $class instance of lib.
It should also be noted that every static function can also be called using an instance of the class but not the other way around.
So this works:
class Foo
{
public static function bar(){}
}
$f = new Foo();
$f->bar(); //works
Foo::bar(); //works
And this doesn't:
class Foo
{
protected $test="fddf";
public function bar(){ echo $this->test; }
}
$f = new Foo();
$f->bar(); //works
Foo::bar(); //fails because $this->test can't be accessed from a static call
Of course you should restrict yourself to calling static methods in a static way, because instantiating an instance not only costs memory but also doesn't make much sense.
This explanation was mainly to illustrate why it worked for you some of the times.
:: is used to access a class static property. And -> is used to access a class instance ( Object's ) property.
Consider this Product class that has two functions for retrieving product details. One function getProductDetails belongs to the instance of a class, while the other getProductDetailsStatic belongs to the class only.
class Product {
protected $product_id;
public function __construct($product_id) {
$this->product_id = $product_id;
}
public function getProductDetails() {
$sql = "select * from products where product_id= $this->product_id ";
return Database::execute($sql);
}
public static function getProductDetailsStatic($product_id) {
$sql = "select * from products where product_id= $product_id ";
return Database::execute($sql);
}
}
Let's Get Products:
$product = new Product('129033'); // passing product id to constructor
var_dump( $product->getProductDetails() ); // would get me product details
var_dump( Product::getProductDetailsStatic('129033') ); // would also get me product details
When to you use Static properties?
Consider this class that may not require a instantiation:
class Helper {
static function bin2hex($string = '') {
}
static function encryptData($data = '') {
}
static function string2Url($string = '') {
}
static function generateRandomString() {
}
}
Sourcing WikiPedia - Class
In object-oriented programming, a
class is a programming language
construct that is used as a blueprint
to create objects. This blueprint
describes the state and behavior that
the created objects all share. An
object created by a class is an
instance of the class, and the class
that created that instance can be
considered as the type of that object,
e.g. a type of an object created by a
"Fruit" class would be "Fruit".
The :: operator accesses class methods and properties which are defined in php using the static keyword. Class const are also accessed using ::
The -> operator accesses methods and properties of an Instance of the class.
If the function operates on an instance, you'll be using ->. If it operates on the class itself, you'll be using ::
Another use of :: would be when you want to call your parent functions. If one class inherits another - it can override methods from the parent class, then call them using parent::function()