Alfraganus Posted December 5, 2015 Share Posted December 5, 2015 Dear all, I am begginner to OO PHP, it is being hard for me to understand it, for example: <? class arraytoobject extends arraytoobject { public function __get($key) { return $this [$key] (why $key should come in bracket here?) } public function __Set($key, $val) (what is val? and why do we need it? why set has 2 arguments and get has only 1 argument?) { $this[$key]=$val; } } ?> would be happy if you would explain me these Link to comment Share on other sites More sharing options...
Ingolme Posted December 5, 2015 Share Posted December 5, 2015 This code is not going to work at all. Where did you find it? First of all, the PHP block should start with "<?php", not "<?". That line might be working now but won't work on all server configurations Second, you cannot extend a class from itself, that doesn't make any sense. I don't believe you can actually call $this[$key]. you should actually be calling $this->$key, or perhaps $this->data[$key]. That line is likely to cause an error. Square brackets [ ] are not an object-oriented thing, they're a basig programming ocnstruct. Square brackets indicate an element in an array, see the chapter on arrays: http://www.w3schools.com/php/php_arrays.asp The reason __set() has two parameters and __get() only has one is that when you want to set a property you need to say the name of the property you want to set and what value you want it to have. When you want to get the value of a property, you just need to say the name of the property you want to get the value of. I don't think your problem is with object-oriented programming, you seem to lack some understanding of basic programming in general. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now