News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

NetTuts.com:
Reflection in PHP
April 19, 2013 @ 10:24:28

On NetTuts.com today there's a new tutorial talking about a part of PHP that can be quite powerful but isn't used too often - reflection in PHP. Using Reflection you can get information about your actual code and its elements without having to try to parse it yourself.

Reflection is generally defined as a program's ability to inspect itself and modify its logic at execution time. In less technical terms, reflection is asking an object to tell you about its properties and methods, and altering those members (even private ones). In this lesson, we'll dig into how this is accomplished, and when it might prove useful.

They provide a little context around the idea of "reflection" in programming languages and then jump right in with a few sample classes. They set up their "Nettuts", "Manager" and "Editor" classes and show how to use the ReflectionClass functionality to get their structure. The examples show how to get the class' methods, their properties and calling these methods using things like invoke and call_user_func.

0 comments voice your opinion now!
reflection tutorial introspection methods parameters invoke

Link: http://net.tutsplus.com/tutorials/php/reflection-in-php

Josh Adell's Blog:
Command Invoker Pattern with the Open/Closed Principle
January 16, 2012 @ 10:04:42

In a response to a recent post on DZone.com about the "Open/Closed Principle" Josh Adell has posted an example of a " flexible and extendable command invocation solution" implementing this SOLID idea.

Let's overcome some of these issues [with only being able to extend the invoker class and that the invoker needs to know how to create commands], and also make the code even more extensible. I'll use a simplified command invoker to demonstrate.

His code is included - the creation of a "Command" interface and two comments that implement it: "HelloCommand" and "PwdCommand", each with "register" and "execute" methods. His "Invoker" class then only needs to be told how to map these commands and the "register" is called as they're needed. You can find the full example code for this invocation example in this gist.

0 comments voice your opinion now!
command designpattern invoke open closed principle solid tutorial


Ryan Gantt's Blog:
Anonymous recursion in PHP
August 11, 2011 @ 10:55:35

In a recent post to his blog Ryan Gantt looks at an interesting way to get around a limitation in PHP dealing with anonymous recursion and closures that throws a Fatal error when called.

Turns out that variables called as functions must be an instance of Closure, an instance of a class which implements __invoke(), or a string representing a named function in the global namespace. In the anonymous function body above, $fibonacci is none of these. It is an undeclared, free variable in the closure created by the anonymous function. At the time when it's called, it hasn't been bound-hence the Notice that you would have gotten if error reporting were set at a high enough threshold - and therefore can't be called as anything, let alone as a function.

He tried using the "use" functionality PHP closures have to bring a variable/object/etc into the scope of the running function, but it still threw an error. As it turns out, the combination of "use"-ing the object and calling it by reference handles things correctly. He takes this method and applies it in two examples - one call in an array_map function and another in an array_reduce.

0 comments voice your opinion now!
anonymous recursion reference invoke closure


Web Developer Juice:
PHP Magic Functions Best Part of Object Oriented PHP - Part 2
May 19, 2011 @ 10:14:27

Web Developer Juice has posted the second part of their series looking at some of the "magic functions" that PHP has to offer - special functions that do automagic things in your scripts and classes. Part one can be found here.

In my previous post ( PHP Magic Functions ), I discussed about __construct, __destruct, __call and __callStatic. Lets explore a few more magic functions...

In this latest part of the series they look at three functions:

  • __set/__get
  • __invoke
1 comment voice your opinion now!
magic function method oop get set invoke


DZone.com:
Reuse your closures with functors
December 29, 2010 @ 10:50:19

On DZone.com there's a new tutorial from Giorgio Sironi about reusing closures with the help of functors (a special kind of object instancing done in PHP with the help of __invoke).

I like PHP closures and their superset, anomyous functions, as they implement the Command pattern very well when used correctly. However I feel that sometimes they are: difficult to reuse and difficult to force contracts on. [...] What if we wanted instead, a closure which we can instance even more than one time (maybe with different variables), and that we could type hint?

He shows how to make this possible with a functor created using the __invoke magic method of PHP to handle the request to an object like a function. He includes some sample code to show it in action - a basic callback (SquareCallback) and how it compares to calling a normal closure. It also shows something a bit more technical, an "AdderCallback" class that can be defined as a type hint on a function.

0 comments voice your opinion now!
closure functor invoke snippet example callback typehint


Jose da Silva's Blog:
Revisiting PHP 5.3 __invoke magic method
November 05, 2010 @ 12:42:04

In a new post to his blog Jose da Silva briefly looks at a feature that was introduced in the PHP 5.3.x series of the language - the __invoke magic method.

PHP version 5.3 introduced a new magic method designed __invoke, this method is called when a script tries to call an object as a function. [...] As php cannot accommodate pseudo-first-class functions, the __invoke method can be used to suppress this language limitation. On other hand you can use this for simpler things as pass a function around.

He includes a simple code example that shows a basic class being called via a variable name - $c('ford') - and the result of its __invoke method being called. He notes that the method, in his opinion, could make for less clean code.

8 comments voice your opinion now!
invoke magic method opinion example


Matthew Weier O'Phinney's Blog:
A Primer for PHP 5.3's New Language Features
April 06, 2010 @ 14:45:00

Whether you're relatively new to the PHP 5.3 scene or just want to be sure you don't miss out on some of the cool new features it has to offer, check out this new guide from Matthew Weier O'Phinney covering the new features of this great release.

Most of the time, these features are straight-forward, and you can simply use them; in other cases, however, we've run into behaviors that were unexpected. This post will detail several of these, so you either don't run into the same issues -- or can capitalize on some of our discoveries.

He talks about some of the strange things he's come across like the fact that PHP doesn't like serializing closures, problems with using the new "__invoke" helper and some picky things that you need to know about namespacing your code.

0 comments voice your opinion now!
php5 feature namespace closure invoke



Community Events





Don't see your event here?
Let us know!


community api interview release voicesoftheelephpant list language series bugfix podcast zendserver conference code framework deployment laravel introduction tips development symfony

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework