Tag Archives: php 5.3

Anonymous functions now available from PHP 5.3

Written by Garry Lachman (Admin). Filed under News, PHP + mySQL. Tagged , , , , , , , , , , , . .

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

Example:

1
2
3
4
$myName = function($name)
{
    echo "My name is $name";
};

Or you can use it as callback like javascript

1
2
3
4
5
6
7
8
9
10
11
12
$arr = array(
    "first_name" => "Garry",
    "last_name" => "Lachman",
    "age" => "27"
);

$callback_function = function ($field, $field_value)
{
    echo "$field = $field_value";
};
       
array_walk($arr, $callback_function);

Using variables out function scope

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$prefix = "*)";

$arr = array(
    "first_name" => "Garry",
    "last_name" => "Lachman",
    "age" => "27"
);

$callback_function = function ($field, $field_value) use ($prefix)
{
    echo "$prefix $field = $field_value";
};
       
array_walk($arr, $callback_function);

Have fun boys & girls :)

Share

Develop PHP Facebook Application Locally – Dev Mode

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , . .

Hi,
As you know Facebook doesn’t have develop mode to that allow you to work locally.
But there is way to do it – and its very easy to deployment.

First create 2 applications on Facebook, one for “dev” and one for “production”.
In dev application set you application url to the locally localhost if “127.0.0.1″ like:

http://127.0.0.1/your_app.

The second step is to create two “environments” in your application – “dev mode” and “production mode”.
Create a settings class that check if you work locally and return the Facebook API Key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
class ApplicationSettings   {
    private static $facebook_api = array(
        "dev"=>array(
            "key"=>"123",
            "secret"=>"123"    
        ),
        "prod"=>array(
            "key"=>"123",
            "secret"=>"123"    
        )
    );

    public static function get_facebook()   {
        if ($_SERVER['HTTP_HOST'] == "127.0.0.1")   {
            return self::$facebook_api["dev"];
        }
        return self::$facebook_api["prod"];
    }
}
?>

Set your Facebook API Key and Secret to the “dev” and “prod” modes.
Now we will change the way we connection to facebook api to get the
settings from our new class.

1
2
3
4
5
6
7
8
<?php
$facebook_settings = ApplicationSettings::get_facebook();

$facebook = new Facebook(array(
    'appId' => $facebook_settings["key"],
    'secret' => $facebook_settings["secret"],
));
?>

Now every time we connect to Facebook API the settings class will
check our domain and if we work on “127.0.0.1″ its automatically
gets the “dev mode” API that pointed to our local application.

Have Fun ;)
Garry Lachman

Share

Garry`s One Time URL PHP5 Script

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , , , , , , , , , , . .


I open Requirements Specification for Advance One Time URL script.
You can see and help with ideas.

Hi,

I wrote little script + lib for one time url.
this script make MD5 hash string for one time using and redirect file.
the links looks like: http://garry-lachman.com/link/ce75f50f55bcedf0a72098a01764548b and can be used one time only.

The url storing is based on PHP Sessions and link redirection on MOD_REWRITE but there is example
for non MOD_REWRITE using
Example of create of the link:

1
2
3
4
5
<?php
require_once("libs/one_time_url.lib.php");
$one_time_url = new one_time_url();
?>
<a href="<?php echo $one_time_url->make_url("http://www.garry-lachman.com"); ?>">This is one time URL to http://www.garry-lachman.com</a>

The code & example can be downloaded form here.
License: GNU/GPL (open source)

Share

CodeIgniter – PHP 5 MVC Framework

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , , , , , . .

I really never used frameworks, i always build my system without any frameworks.
but no more… after few days with CodeIgniter i`m in Love…

CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. If you’re a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you’re tired of ponderously large and thoroughly undocumented frameworks”

CodeIgniter

Have a nice day ;)

Garry Lachman

Share

PHP Tips – Manage correctly file inclusion

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , . .

“In this article we’ll treat a quite simple argument but if badly managed can lead to big problems, the topic is the inclusion of files.
According to my experience I established three golden rules that should be enough; if you come up with other ones, I’ll be glad to talk about it….”

Read full article here

Share

“ereg is deprecated” warnings in PHP 5.3

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , , , , , , . .

If you try to install systems like OSCommerce on PHP 5.3.x you may got strange
error like “ereg is deprecated…”.

Here is the solution!!!

1
ereg('RegExp', $x $y);

becomes

1
preg_match('/RegExp/', $x $y);

dont forget “/ /” between the regexp code!!!

Same for “ereg_replace”

1
ereg_replace('RegExp', $x, $y);

becomes

1
preg_replace('/RegExp/', $x, $y);

Thanks,
Garry Lachman

Share