<?php class MyClass { public $prop1 = "I'm a class property!"; public function setProperty($newval) { $this->prop1 = $newval; } public function getProperty() { return $this->prop1 . "<br />"; } } // Create two objects $obj = new MyClass(); $obj2 = new MyClass(); // Get the value of $prop1 from both objects echo $obj->getProperty(); echo $obj2->getProperty(); // Set new values for both objects $obj->setProperty("I'm a new property value!"); $obj2->setProperty("I belong to the second instance!"); // Output both objects' $prop1 value echo $obj->getProperty(); echo $obj2->getProperty();
--> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <?php class MyClass { public $prop1 = "I'm a class property!"; public function setProperty($newval) { $this->prop1 = $newval; } public function getProperty() { return $this->prop1 . "<br />"; } } $obj = new MyClass(); echo $obj->getProperty(); // Get the property value $obj->setProperty("I'm a new property value!"); // Set a new one echo $obj->getProperty(); // Read it out again to show the change ?> </body> </html>
public $prop1 = "I'm a class property"; public function setProperty($newval) { $this->prop1 = $newval; } public function getProperty() { return $this->prop1 . "<br />"; } } $obj = new MyClass(); $obj2 = new MyClass(); echo $obj->getProperty(); echo $obj2->getProperty(); $obj->setProperty("Son! You're close to becoming a developer"); $obj2->setProperty("Son! Welldone!"); echo $obj->getProperty(); echo $obj2->getProperty(); //$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); //echo fread($myfile,filesize("webdictionary.txt")); //fclose($myfile); //Using Class Inheritance class MyOtherClass extends MyClass { public function __construct() { /* Preserving Original Method Functionality While Overwriting Methods To add new functionality to an inherited method while keeping the original method intact, use the parent keyword with the scope resolution operator (::): */ parent::__construct();