file: ClassUser.php
[cc lang=”php” escaped=”true” width=”500″]
setExample(“example sucks”);
echo($class_instance->getExample());

$class_instance->NewProperty = “You just created a new propertey of the class, that is not defined per default”;
$class_instance->AnotherProperty = “You just created a new propertey of the class, that is not defined per default”;

?>
[/cc]
file: ExampleClass.php
[cc lang=”php” escaped=”true” width=”500″]
example = $data;
$this->another_var = $data;
}

public function getExample() {
return $this->example;
}

public function setExample($example) {
$this->example = $example;
}

public function exampleFunction() {
$test = $this->example; // now that you have a private $example, this will work
$another_test = $this->another_var; //this obviously does
}

/* let’s you dynamically generate new properties */
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}

public function __set($property, $value) {
if (!property_exists($this, $property)) {
$this->$property = $value;
}

return $this;
}

}
?>
[/cc]

output is:

"$class_instance" (ExampleClass)
-> another_var = 
-> example = example sucks
-> NewProperty = You just created a new propertey of the class, that is not defined per default
-> AnotherProperty = You just created a new propertey of the class, that is not defined per default

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin