Implicit Class Variable Declaration in PHP

implicit class variable declaration in php?

This works the same as a normal variable declaration would work:

$foo = 'bar'; // Created a new variable

class Foo {
function __construct() {
$this->foo = 'bar'; // Created a new variable
}
}

PHP classes are not quite the same as in other languages, where member variables need to be specified as part of the class declaration. PHP class members can be created at any time.

Having said that, you should declare the variable like public $foo = null; in the class declaration, if it's supposed to be a permanent member of the class, to clearly express the intent.

Why implicit property declaration in PHP?

The use-case are value objects

$a = new stdClass;
$a->something = 12;
$a->somethingElse = 'Hello World';
myFunction ($a);

This allows to create some objects, that just carry around structured data (something like structs in other languages), without the need to define a class for it.

Another point is, that -- because PHP is weakly-typed anyway -- there is no real reason to forbid it. If you need something stronger, overweite __set()

public function __set ($propertyName, $value) {
throw new Exception("Undefined Property $propertyName");
}

PHP Class Variable Declaration with Assignment Failing

The properties are a blueprint and must be independent of the runtime environment. You can set them to literal values but you can't make function calls like that. Set the initial properties in the constructor:

class session {

private $variable;
private $cookieExpiry;

function __construct() {
$this->cookieExpiry = time() + 15811200; // Cookie Expire
}
}

What is the purpose of declaring variables in a class

Both variants are correct but the second one is lacking when getting testvar before you initialize it.

If you call $test->getTestvar(); before you set it with $test->setTestval('bla bla');, you will get a warning, something like:

Notice: Undefined property: Testclass::$testvar

The second variant also lacks the property visibility part (i.e. private, protected). More about visibility.

The declaration of class properties above methods is a good practice, it's taken from strict oop-languages like java.

How to enforce initialization of class variables without a constructor?

Not exactly an answer to your question but maybe a solution for your problem:

Instead of having a property containing the actual class name (that's what you wanted, isn't it?) you can use get_called_class() (as of php 5.3.0) to get the name of the class for which the static function has been called.

 class Entity {
public static function foo() {
echo 'name=', get_called_class(), "\n";
}
}

class ClassA extends Entity { }
class ClassB extends ClassA { }

Entity::foo();
ClassA::foo();
ClassB::foo();

prints

 name=Entity
name=ClassA
name=ClassB

Have $this in PHP implicit function before PHP 5.4.0

Should do the work:

$object = $this ;
$callback = function() use ($object) { return $object->my_callback(); } ;

use will bring an accessible variable (in our case the reference of the object) upon its declaration to the function scope, so you will not have to send it as a parameter.

Sometimes it is even better to use such a varname as $self or $that so to be more clear.

without defining public property is accessible using $this in php OOP

i got the concept.

PHP classes are not quite the same as in other languages, where member variables need to be specified as part of the class declaration. PHP class members can be created at any time.

Implicit Type Conversion for PHP Classes?

Long answer:

I think it is very difficult (read impossible) for PHP to do an implicit conversion in this case.

Remember: the fact that you call your class Integer is a hint to the human reader of the code, PHP does not understand that it actually is used to hold an integer. Also, the fact that it has an attribute called $val is a hint to a human reader that it should probably contain the value of your integer. Again PHP does not understand your code, it only executes it.

At some point in your code you should do an explicit conversion. It might be possible that PHP has some nice syntactig sugar for that, but a first attempt would be something like:

class Integer
{
public $val;

function __construct($val) { $this->val = $val; }
}

function ExampleFunc($i){
if (is_numeric($i)) { $iObj = new Integer($i); }
...
}

ExamFunc(333); // 333 -> Integer object with $val === 333.

This is not as cool as you would like it, but again, it is possible that PHP has some syntactic sugar that will hide the explicit conversion more or less.

Short version:

In one way or another, you will need an explicit conversion



Related Topics



Leave a reply



Submit