Friday, December 17, 2010

Re: Defining php functions in views

@euromark - He answered that in his post.

@ojonam - Scope issure for sure, just do.

function getLink($view, $x) {
return $view->Html->link($x,"#".$x);
}

echo getLink($this, $x);

On Dec 17, 4:47 am, euromark <dereurom...@googlemail.com> wrote:
> is there a reason why you dont want to stick to the cake pattern
> and create helpers for that?
>
> On 17 Dez., 13:41, Ryan Schmidt <google-2...@ryandesign.com> wrote:
>
> > On Dec 17, 2010, at 05:15, ojonam wrote:
>
> > > $getLink = function($x){return $this->Html->link($x, '#'.$x);};
> > > $alphabet = array_map($getLink, array_merge(range('a','z')));
>
> > In PHP 6.x or possibly 5.4.x you should be able to do this:
>
> > $getLink = function($x)use($this){return $this->Html->link($x, '#'.$x);};
> > $alphabet = array_map($getLink, array_merge(range('a','z')));
>
> > However, it's not possible for an anonymous function to use "$this" in PHP 5.3.x; see this bug report:
>
> >http://bugs.php.net/bug.php?id=49543
>
> > Until then, you have to copy $this to a temporary variable; here, I copy it to "$view":
>
> > $view = $this;
> > $getLink = function($x)use($view){return $view->Html->link($x, '#'.$x);};
> > $alphabet = array_map($getLink, array_merge(range('a','z')));

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments:

Post a Comment