Spiga

‘ projects ’ category archive

[phpclass] dinamicParams – Calling function with named params

November 23, 08 by Gabi Solomon

A lot of time while developing a function or a method of a class you need to specify 1, 2 or 3 parameters as inputs for that function or method. But usually as you develop further you might end-up in the situation where you would want to add a new parameter to your function.

And so you might have coded a function that has 5 or more parameters, and although it works fine, eventually you will want to call that function and leave the first 4 parameters with the default value and only change the last one. I ended up here a lot of times, and normally in php there is nothing you can do.

A workaround would be to specify the params as a single array variable, but that is just making you write more code and it looks really bad ( at least in my opinion).

Well today i found a new method of doing this in php5 with the help of the Reflection API that is offer in php5. I will not get into to much details about what it does, you can read that in the documentation, basicly it provides a way to reverse-engineer classes, interfaces, functions and methods.

So i wrote a simple class that is going to solve that problem.

With the help of the Reflection API from php5 the class will allow you to call function and methods by providing the parameters as an array specifying names and values even if the original function or methods receives them in the normal fashion.

But enough talk, heres the example.

PHP:
  1. <?php
  2. require('dinamicParams.class.php');
  3.  
  4. class news {
  5.  
  6.     function fetch ( $id, $page = 0, $items_pp = 10, $order_by = 'date', $method = 'desc') {
  7.         echo "
  8.             id: $id,
  9.             page: $page,
  10.             items_pp: $items_pp,
  11.             order_by: $order_by,
  12.             method: $method
  13.         ";
  14.     }
  15.  
  16. }
  17.  
  18.  
  19.  
  20. $news = new news();
  21. $dinamicParams = new dinamicParams();
  22.  
  23. $dinamicParams->call(array($news, 'fetch'), array('order_by' => 'name'));

This will output :

id: , page: 0, items_pp: 10, order_by: name, method: desc

This class is based on this implementation of the reflection API.
It is available on phpclasses can be downloaded here

how to: Generating XML from PHP

August 15, 08 by Gabi Solomon

I had to do a small REST application on a project recently and i wanted to find a small easy way of returning the response in XML.

After a little searching find this rather old post ( but just what i was looking for ) on Simon Willison’s Weblog. It featured a small php class to generate XML.

Example Code

PHP:
  1. $array = array(
  2.     array('monkey', 'banana', 'Jim'),
  3.     array('hamster', 'apples', 'Kola'),
  4.     array('turtle', 'beans', 'Berty'),
  5. );
  6. $xml = new XmlWriter();
  7. $xml->push('zoo');
  8. foreach ($array as $animal) {
  9.     $xml->push('animal', array('species' => $animal[0]));
  10.     $xml->element('name', $animal[2]);
  11.     $xml->element('food', $animal[1]);
  12.     $xml->pop();
  13. }
  14. $xml->pop();
  15. print $xml->getXml();

XML returned

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <zoo>
  3.   <animal species="monkey">
  4.     <name>Jim</name>
  5.     <food>banana</food>
  6.   </animal>
  7.   <animal species="hamster">
  8.     <name>Kola</name>
  9.     <food>apples</food>
  10.   </animal>
  11.   <animal species="turtle">
  12.     <name>Berty</name>
  13.     <food>beans</food>
  14.   </animal>
  15. </zoo>

Attention

A little heads up, if your running php you might encounter this error:

Fatal error: Cannot redeclare class xmlwriter in /var/www/web4/cgi-bin/includes/XmlWriter.class.inc on line 6

Thats because php already has a built in php class called xmlwriter, to fix it all you need to do is rename it.

hope this helps somebody.
Cheers

New project Launch – YM Archive

August 02, 08 by Gabi Solomon

I have been writing a small website in the small free time i have had for the past 2 months, and testing it to see how it will perform if many users will start to use it and now i believe i can make it public, at least as pre-alpha state :D .

The website is called YM Archive and the ideea is of an archive that will keep a track of a users activity on Yahoo messenger ( online times and avatars ). It only records the online time, not the invisible time as i value the users privacy ... if he wants to be invisible he must have a reason to do it.

The website was originaly build for me, since many times i wanted to know if a person has been online today and i have missed him, but i figured maybe others will want the same information ... so make it a public service.

As a pointer, the website only tracks a limited amount of users, the ones that it has in its database. To add a new user in the database you need to do a search for it and it will add it automatically, and from that point it will track its status, so next time you look the same ID up it should have some history for you ( if the user has been online since add ).

In time the website will integrate other IM services, and maybe include an user account where you will have some stats printed out for you and your friends.

Hope you enjoy it.
Cheers

PS: I am curious to see when will my hosting close my account for to many curl connections :) )

lyrics database website v.09

July 31, 08 by Gabi Solomon

I have recently started a new website, its a lyrics database and it is accessible at http://lyrics.pixie-search.com/.

It is currently in alpha phase, but it will have an added features compared to other websites like videos crawled from youtube, and translation for lyrics using google translate service.

That is it for now,
Hope you will like it.

Want to build an application to update all major social services ?

July 24, 08 by Gabi Solomon

As social networks seem to get bigger and bigger and new ways of online communication spring up, people get more and more caught up into updating their accounts on various networks.

So wheres there's a need there's an application :) .

As i was talking a few days ago a new service has been borne a few months ago called ping.fm that allows users to update their services all from one place. Well today i stumble upon a class on phpclasses.org called Ping.FM Wrapper that can be used to update multiple social networks using the Ping.FM API.

It accesses the Ping.FM Web services API server and can perform several operations to update social networks of a given user. It can for instance post messages or the user status, retrieve received messages, etc..

I guess this will make the life of developers required to do an application that will update social networks a whole lot easyer. :)

Cheers

Google Translate Tool Class v0.9

July 10, 08 by Gabi Solomon

As almost all of my classes i have developed this class for a project requirement, and i decided to make it public on phpclasses.com that is available for download at this link.

This is quite a simple class can translate text, and webpages using Google translate services.

The class usses curl to make a tranlation request to the Google translate Web page and asking for the translation of the given text or URL from the selected from and to languages. The result is parsed to obtain the translation and returned by the class.

The translation class supports all the languages currently available at google including:
English, German, Spanish, French, Italian, Portuguese, Arabic, Chinese and Russian.