3

I created a new object-level variable due to a typo. This was tough to debug. While this could be a lazy feature, is there a way to prevent object-level variables from being created by an assign? I was hoping the error_reporting would at least tell me I did it, if not prevent it.

<?php

error_reporting(E_STRICT|E_ALL);

class SearchField {
    var $field1;
}

$field = new SearchField();
$field->field1 = 'value1';
echo '<p>'.var_dump($field).'</p>';

$field->feild1 = 'value2';
echo '<p>'.var_dump($field).'</p>';

$field = new SearchField();
$field->field1 = 'value1';
echo '<p>'.var_dump($field).'</p>';


?>

Here, the typo is fairly obvious. Sometimes it not so obvious.

object(SearchField)#1 (1) { ["field1"]=> string(6) "value1" }
object(SearchField)#1 (2) { ["field1"]=> string(6) "value1" ["feild1"]=> string(6) "value2" }
object(SearchField)#2 (1) { ["field1"]=> string(6) "value1" }
1

1 Answer 1

2

You could use a magic setter:

public function __set() {
    throw new LogicException('Thou shalt not create variables on me');
}

http://www.php.net/manual/en/language.oop5.overloading.php#object.set

Though I'd question how good an idea it is to add this to all your classes just to be defensive about this one possible mistake. You should rather use protected properties and explicit getter and setter methods and get in the habit of using $field->setField('foo'), which is better OOP practice to begin with.

Sign up to request clarification or add additional context in comments.

4 Comments

Well, protected/private won't prevent creating new properties such way, but - definitely, using this "protection" mechanism isn't a good idea for all cases. Because it has little sense. If class has some API - so it should use it and the rest things should be out of it's concern (unless they're conflicting with some other things inside instance, of course)
@Alma "...and get in the habit of using [setters]". If you do that, you'll see very verbose error messages if you fat-finger a setter. Yes, it won't prevent creation of properties, but it's pretty irrelevant if your class has a few useless properties set on it when you're using getters and setters for everything.
Many thanks. I'll view this more as a debugging technique than coding practice. Of course, using good practices should be obvious when writing new code (and should always be welcome advice). But, when one is handed a 10-year old script with a class that has dozens of attributes with poor naming conventions and thousands of lines in a dozen or so methods to make just a few changes, rewriting isn't always an option.
@deceze using getter/setter antipattern or protected has actually nothing to do with "better OOP" - softwareengineering.stackexchange.com/questions/21802/… and softwareengineering.stackexchange.com/questions/134097/… ; also, there's nothing inherently wrong with properties that's not wrong with getters/setters (see C#); there's no real gain in "OOPing" POPO or other entity-type objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.