Re: [SMARTY] implementation-idea for registering plugins as class-methods

From: Date: Thu, 13 Feb 2003 16:30:34 +0000
Subject: Re: [SMARTY] implementation-idea for registering plugins as class-methods
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-7736@lists.php.net to get a copy of this message
On Thu, Feb 13, 2003 at 04:54:12PM +0100, Matthias Mohr wrote: > > > Here is an idea for the problem, that you cannot use class-methods > > > as plugin-functions. > > > > > > I had a look at the Smarty source-code. > > > To check the existance of a given functionname, you only check it with > > > "function_exists". > > > In my opinion you have to check it first with > > > method_exists( $this, funcname ) > > > if it does not exists run through the whole object-parents with > > > get_parent_class(...) and to the same method_exists-call with the parents > > > until you either found a method with the given name or until there are no > > > more parents. > > > > this is unnecessary. a subclass inherits all methods of it's parent-class. > > so a parent-class cannot have a method, that is unknown to it's children. > I think it is necessary. > "method_exists" only returns true, if the method exists in the given object, > not if it exists as derive from a parent class. you are mixing classes and objects here. > So to check the existance of the given function, you have to walk through the > object parents until you find it... no. class A { function f_a() { } } class B extends A { function f_b() { } } $obj = new A; printf("f_a in A %d\n", method_exists($obj,'f_a')); printf("f_b in A %d\n", method_exists($obj,'f_b')); $obj = new B; printf("f_a in B %d\n", method_exists($obj,'f_a')); printf("f_b in B %d\n", method_exists($obj,'f_b')); gives: f_a in A 1 f_b in A 0 f_a in B 1 f_b in B 1 method f_a exists in instances of class B. see? > > > Would this be hard to implement? > > > It would solve all the problem and would also be a great step forward! > > > > i think it would be hard. > For my first look it look relatively simple > (change all calls of function_exists to an own function; > extend the _plugins-array, so that it can additionally contain an object; > change the execution of the plugins to check for that object and use it) > But I have no deep insight into the Smarty-sources/structure, so you surely > know it much better. not necessarily. give it a try if you need it that bad. > > why do you want to put everything inside a subclass of class Smarty > > anyway ?? > Because > - I like to have everything encapsulated and but if you "encapsulate" everything inside an instance of Smarty (or a subclass of it) you gain nothing. everything is encapsulated on a big heap. > - I want access to the methods and variables of the derived class(es) and you can, except for modifiers. i agree with you, that for the sake of consitency modifiers should also receive a $smarty-object. > - additionally I think it's much more consistent, that an existing class method > inside the current object will be called (if it exists) instead of a global function. i think that is a matter of taste. > > with the lack of protected variables and with php's limited > > oop-capabilities things can get very complicated und nearly > > undebuggable with deep class-hierarchies and all-in-one objects that > > provide functionality for everything. > I can't contradict, but it mustn't be so complicated, that it leads to problems... > Either I use oop-capabilites or not - but I try not to mix it. > So if I have a class and a functionality which this class represents I try to > put the whole functionality in it and not to put some parts outside. > > So I'm able to inherit from the Smarty-class, but it's not possible to use > it consequently. > That's the thing I criticize! yes, you are able (==allowed) to extend the class Smarty. but you shouldn't need to for (i guess) >90% of applications for smarty. you should write plugins to extend (not in the oo-meaning here) smarty. > > smarty knows register_object() since 2.4.0 and > > method-calls to those objects. i would try using these first. > You mean to register my objects and then access them inside the plugins? > How will this work? no. it would be possible, though. via $smarty->_reg_objects from your plugins. but since $_reg_objects is considered private this is "illegal" and definately disencouraged. > Or to register one plugin-object and then access that methods instead of > using the plugin-functionality? > This is in my opinion also only a workaround. maybe this is the reason why smarty is sometimes called "The compiling template engine" and not "The object-oriented template engine" as you want it to be. :-) > Then I'll use global function-wrappers instead. i'm sorry that you'll have to for now. have fun messju > with regards, > Matthias

« previous php.smarty.general (#7736) next »