phant0m Posted June 14, 2010 Share Posted June 14, 2010 As the topic title suggests, how does PHP deal with object properties that has the same name as a method? class Test{ protected $isTesting = true; public function isTesting(){ var_dump($this->isTesting); } } $test = new Test; $test->isTesting(); But what if I wanted to run a function with the name that is stored in the property? The code above behaves as intended, it outputs "true". Can this naming be used without concern? Or are there any pitfalls? Quote Link to comment https://forums.phpfreaks.com/topic/204720-class-member-with-same-name-as-method/ Share on other sites More sharing options...
Mchl Posted June 14, 2010 Share Posted June 14, 2010 But what if I wanted to run a function with the name that is stored in the property? Like this? $method = 'isTesting'; $test->$method(); or do you have something else in mind? Quote Link to comment https://forums.phpfreaks.com/topic/204720-class-member-with-same-name-as-method/#findComment-1071783 Share on other sites More sharing options...
ignace Posted June 14, 2010 Share Posted June 14, 2010 $this->isTesting; $this->isTesting(); Are two different things at a memory-level. You only fool yourself using an approach like this use getIsTesting(), setIsTesting() instead. Quote Link to comment https://forums.phpfreaks.com/topic/204720-class-member-with-same-name-as-method/#findComment-1071798 Share on other sites More sharing options...
phant0m Posted June 14, 2010 Author Share Posted June 14, 2010 But what if I wanted to run a function with the name that is stored in the property? Like this? $method = 'isTesting'; $test->$method(); or do you have something else in mind? Yes, something like this: $this->type = 'SomeClass'; $some_class = new $this->type('Constructor Param'); I know that this works^^ - but was if there was a method with the same name? public function type($param){} @ignace, I guess you're right. But do you know where I can read up on this? Quote Link to comment https://forums.phpfreaks.com/topic/204720-class-member-with-same-name-as-method/#findComment-1071802 Share on other sites More sharing options...
Mchl Posted June 14, 2010 Share Posted June 14, 2010 I can't see why would it stop working... Anyway, as ignace points out, it is introducing confusion. One popular convention for boolean properties and their getter/setter method is as follows class SomeClass { private $booleanVal; public function isBooleanVal($value = null) { if(!isset($value)) { return $this->booleanVal; } else { $this->booleanVal = (bool)$value; } } } $obj = new SomeClass(); $obj->isBooleanVal(true); var_dump($obj->isBooleanVal()); This might be less confusing, although some people don't like getter/setter functionality stacked in one method. Quote Link to comment https://forums.phpfreaks.com/topic/204720-class-member-with-same-name-as-method/#findComment-1071811 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.