Use Static instead of the Globals

Oh no. php global are now deprecated in PHP!
how to refactory my code?

So, after about half day. I found the way.

Why globals or static? 


# I need something like 'reuse' not 'requery';
# I dont want query user profile in every function i need the data.

function get_current_username()
{
//do query user profile for the username
}

function get_current_picture()
{
//do query user profile for the picture
}

# I dont want mess my function with the same argument

function get_current_username($current_user)
{
}

function get_current_picture($current_user)
{
}

So with the php globals, we can skip requery like this

global $current_user;

$current_user = get_currentUser();

function get_current_username($current_user)
{
global $current_user;
return $current_user['username'];
}

function get_current_picture($current_user)
{
global $current_user;
return $current_user['picture'];
}


but for me to implement the php static, i need to refactor my codes.

Using the static in classes can be called as 'singleton'.
But you guys can read more about singleton
here.

See Simple Basic Singleton here

Popular Posts