OOP Visibility, in general and specifically in PHP -



OOP Visibility, in general and specifically in PHP -

this general oop question, i'm using php, if exists in general, i'd know, give me warning if know it's missing or warped in php.

is possible class instantiates object of class (which not extending) set visibility of instantiated object's properties?

example:

class widget { public $property1; public $property2; public $property3; } class clockmaker { public function getwidget() { $this -> widget = new widget(); $this -> widget -> property1 = "special stuff"; } } $my_clock = new clockmaker(); $my_clock -> getwidget(); $my_clock -> widget -> property2 = "my tweak"; // totally fine , expected... $my_clock -> widget -> property1 = "my stuff"; // should throw error...

as far understand, if set property1 protected, clockmaker won't able set value. if stands right now, $my_clock can have it's way widget object.

anyway prevent property beingness set 1 time it's been set?

your code doesn't alter property , getwidget never called. create widget property in $my_clock object, , create object empty value (if enable strict errors, you'll see strict notice). declare widget in class:

class clockmaker { private $widget; public function getwidget() { $this -> widget = new widget(); $this -> widget -> property1 = "special stuff"; } }

php oop visibility

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -