Home > Software design >  assigning undefined class variable outside of the class!! - php
assigning undefined class variable outside of the class!! - php

Time:12-04

I don't understand this.

I have an empty class, and I can define a variable belonging to the class and assign values to it outside of the class!! how is it possible?

<?php

class Test{}

$test = new Test();

var_dump(isset($test->foo));
$test->foo = 'bar';
var_dump(isset($test->foo));

echo $test->foo;

The result is as follows:

bool(false)
bool(true)
bar    

someone pleases explain it. is it even safe that php has such a feature?

CodePudding user response:

in PHP < 8.2 it was allowed to dynamically assign properties to a class, in PHP 8.2 it will be depricated https://stitcher.io/blog/deprecated-dynamic-properties-in-php-82

  • Related