Home » Php » class – php adding field on the fly without defining them first

class – php adding field on the fly without defining them first

Posted by: admin July 12, 2020 Leave a comment

Questions:

so in php, you don’t have define the field in a class first like java , is that correct??

in java you cannot say

public class javac {
    int x;
    int y;
    public javaC() {
     this.z = 3;
    }
 }

but in php can you say that?

class phpC {
$x;
$y;

public phpC() {
   $this->z = "omg"; 
 }
  }

what is php’s documentation on this? and what is the correct term for such behaviour, i dont think it is called adding field on the fly

How to&Answers:

You are correct.. You can assign a value to an uninitialized property in PHP. Java is strict and PHP is loosey goosey 🙂

The technical name for this is “overloading.” Here is the documentation:
http://php.net/manual/en/language.oop5.overloading.php