Source Code:
(back to article)
<?php // Define a new class called MyClass class MyClass { /** * @var int */ // Define a private property called "myProperty" and initialize it to 0 private int $myProperty = 0; // Define a public method called "getMyProperty" that returns the value of "myProperty" public function getMyProperty(): int { return $this->myProperty; } // Define a public method called "setMyProperty" that takes an integer value and sets "myProperty" to that value public function setMyProperty(int $value): void { $this->myProperty = $value; } } // Create a new instance of MyClass called "myObject" $myObject = new MyClass(); // Call the "getMyProperty" method on "myObject" and store the result in a variable called "value" $value = $myObject->getMyProperty(); // Output the value of "value" echo "The initial value of myProperty is: " . $value . "\n"; // Call the "setMyProperty" method on "myObject" and set "myProperty" to 42 $myObject->setMyProperty(42); // Call the "getMyProperty" method on "myObject" again and store the result in "value" $value = $myObject->getMyProperty(); // Output the new value of "value" echo "The new value of myProperty is: " . $value . "\n";
Result:
Report an issue