Yii Framework Forum: Are these kinds of things possible? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Are these kinds of things possible? Rate Topic: -----

#1 User is offline   JamesBarnsley 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 18-November 10
  • Location:UK

Posted 03 April 2012 - 01:00 PM

Similar to the Ruby way, I was wondering with the annonymous functions / closures etc.

$books->each = function($book) {

echo $book->name;

}

Is this kind of thing possible in PHP yet? If so could we see the features going more like this in Yii?
0

#2 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 03 April 2012 - 02:04 PM

Well, if they are possible with PHP, they are possible with Yii as well. As for your example: No, I don't think closures work that way.
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#3 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 03 April 2012 - 10:18 PM

foreach($books as $item){
    echo $item->name;
}

2

#4 User is offline   ololo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 23-January 11
  • Location:Minsk, Belarus

Posted 04 April 2012 - 03:44 AM

array_walk($books, function($book) { echo $book->name; });


or

$echoName = function(Book $book) { 
    echo $book->name; 
};
array_walk($books, $echoName);

0

#5 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,735
  • Joined: 17-January 09
  • Location:Russia

Posted 05 April 2012 - 10:23 AM

foreach is the best in this case :)
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users