Skip to content

Object: helper for protecting method from multiple invocation #646

Closed
wants to merge 1 commit into from

3 participants

@fprochazka

Just a helper, for inject*() methods.

Any ideas? Related to [#638]

@fprochazka fprochazka commented on the diff May 10, 2012
Nette/common/ObjectMixin.php
@@ -241,4 +244,26 @@ public static function has($_this, $name)
return isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name]);
}
+
+
+ /**
+ * Ensures, that the desired method will be called only once.
+ * @param object instance that should be protected
+ */
+ public static function freezeMethod($object)
+ {
+ $frozen =& self::$frozenMethods[spl_object_hash($object)];
+ foreach (debug_backtrace() as $call) {
@fprochazka
fprochazka added a note May 10, 2012

It would be nice, if it could be done without this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dg
Nette Foundation member
dg commented May 10, 2012

I think it could be ensured only by contract. Method inject() is de facto an extension for method __construct() and constructor can also be called multiple times but nobody wants to check it.

@juzna juzna commented on the diff May 10, 2012
tests/Nette/common/Object.freezeMethod.phpt
+ */
+
+
+
+
+require __DIR__ . '/../bootstrap.php';
+
+
+
+class TestClass extends Nette\Object
+{
+ public $context;
+
+ public function injectContext(Nette\DI\Container $context)
+ {
+ $this->freezeMethod();
@juzna
juzna added a note May 10, 2012

code: "freezeMethod()"
me: "wtf, which method"?
phpdoc: "the desired method"
me: "wtf, which desired, me didn't pass anything"

I'd rather be explicit and pass the __FUNCTION__ argument there ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fprochazka

@dg I can live with that. Contract is fine for me, but there are people, that are gonna complain about "proprietary conventions" ;)

@dg
Nette Foundation member
dg commented May 10, 2012

I do not like proprietary conventions too, so ideal solution is to add suitable phpDoc for injection method, something like "call after __construct and never more".

@fprochazka

Ok, this can be closed than.

@fprochazka fprochazka closed this May 10, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.