It is possible to unset even object properties visible in current context.

function.unset.php

Note:
It is possible to unset even object properties visible in current context.

class MyClass {

public $property = 'value';

public function unsetProperty()
{
unset($this->property);
}

public function dump()
{
var_dump($this->property);
}
}

$object = new MyClass();
$object->unsetProperty();
$object->dump(); // PHP Notice: Undefined property: MyClass::$property in

Str_Affix


class Str_Affix {

    public static function __callStatic($name, $arguments)
    {
      return (string) isset($arguments[0]) ? $arguments[0] : "";
    }
    public static function prefix($original, $affix){
      return (string) $affix . $original;
    }
    public static function suffix($original, $affix){
      return (string) $original . $affix;
    }
    public static function circumfix($original, $affix){
      if(is_array($affix) && count($affix) == 2){
        return (string) $affix[0] . $original . $affix[1];
      }
      return (string) $original;
    }
    public static function simulfix($original, $affixes, $phonemes){

      if(!empty($phonemes)){
        return (string)str_replace($phonemes, $affixes, $original);
      }
      return (string) $original;
    }
}

function array_affix_keys($affix, Array $array, $type = 'prefix', $phonemes = null){

    array_walk($array, function(&$value, &$key) use ($type, $affix, $phonemes) {
        $key = call_user_func(array('\Str_Affix', $type), $key, $affix, $phonemes);
    });
    return $array;
}
$array = array_combine(range('a','z'), range('a','z'));
$prefix = array_affix_keys('_', $array, 'prefix');
$suffix = array_affix_keys('_', $array, 'suffix');
$circumfix = array_affix_keys(array('_','.'), $array, 'circumfix');
$simulfix = array_affix_keys(array('A','E','I','O','U'), $array, 'simulfix', array('a', 'e','i','o','u'));
var_dump($prefix);
var_dump($suffix);
var_dump($circumfix);
var_dump($simulfix);

Stupidly Simple SSH Key Management

This ssh_keygen function creates a key pair for a given user@host, it then copies the public key to your clipboard ready for you to add your authorized keys.

function ssh_keygen(){
    ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to $1" -f ~/.ssh/"$1_id_rsa"
    echo "~/.ssh/"$1_id_rsa Created"
    echo "~/.ssh/"$1_id_rsa.pub Created"
    pbcopy < ~/.ssh/"$1_id_rsa.pub"
} 
#pbcopy alias for linux, requires xclip
hash pbcopy >/dev/null 2>&1 || alias pbcopy="xclip -selection clipboard"

Usage

$ ssh_keygen user1@tarranjones.com

Output

$ ~/.ssh/user1@tarranjones.com_id_rsa  Created
$ ~/.ssh/user1@tarranjones.com_id_rsa.pub  Created

~/.ssh/user1@tarranjones.com_id_rsa.pub is then copied to your clipboard, it should look something like this.

ssh-rsa AAA...AB3Nz== tarran@Tarrans-Machine to user1@tarranjones.com

As you can see, ssh_keygen adds a comment at the end of the key to make it easier to recognise which keys are for what, this can be useful when deleting old unused keys.

Now to tell ssh where to find the correct files.

Simply modify your ~/.ssh/config File to add an IdentityFile option to all hosts.
~/.ssh/config

Host *
IdentityFile ~/.ssh/%r@%h_id_rsa

Thats it.

If you want to add a key for use with your GitHub account thats simples

$ ssh_keygen git@github.com

Test the Connection with

ssh -T git@github.com

What if you have multiple Github accounts?

git@github.com is the only user@host combination used to ssh to github.com
Somehow you’ll need to tell git which account you wish to use and where it can find the relevant IdentityFile

Take a look at SSH git key management to see how its done.

SSH git key management

All my ssh keys have the naming convention (remote user name)@(remote host name)_id_rsa
(More details on how and why here)

Most git hosts require authenticating as ‘git‘, so to ssh to GitHub git would do the internal command.

ssh git@github.com

This would attempt to authenticate using the ssh key contained within the ~/.ssh/git@github.com_id_rsa identity file.
If you have multiple github accounts and wish to use different keys for each then git will need to be told where to find the correct key.

The git repository in which you currently reside ( $ pwd ) will determine which account you will want to use so it makes sense to hold this information here.
Every git repository can hold local config variables, one variable git uses is core.sshCommand, this could be used to enable debugging.

git config --local core.sshCommand "ssh -vvv "

Or in our case specify a identity file.

git config --local core.sshCommand "ssh -i ~/.ssh/git@github.com:tarranjones"

Of course this would require setting this local variable for every repository.

An alternative way to target the correct identityFile which requires configuring less often would be to use a combination of global and local variables.
As well as my global user.name and user.email variables I also have a user.username variable.

git config --global user.username "tarranjones"

All my --global user. variables represent my most frequently used account details, when using a repository with a different account I change the details locally.

git config --local user.username "otheraccountusername"

And now to set the core.sshCommand variable globally, for the naming convention (~/.ssh/user@host:username_id_rsa) you would do.

git config --global core.sshCommand "ssh -i ~/.ssh/\$1:$(git config --get user.username)_id_rsa"

All git hosts which I currently use authenticate using ‘git‘ as the remote user name. I have omitted it from the identityFile filename and replaced it with the git account username (~/.ssh/username@host_id_rsa).

To use this naming convention you would do the following instead.

git config --global core.sshCommand "ssh -i ~/.ssh/\$(git config --get user.username)@\${1#*@}_id_rsa"

All together

Initial setup


ssh-keygen -t rsa -b 4096 -f ~/.ssh/tarranjones@github.com_id_rsa
git config --global user.username "tarranjones"
git config --global core.sshCommand "ssh -i ~/.ssh/\$(git config --get user.username)@\${1#*@}_id_rsa"

Day to day

clone your repo

$ git clone git@github.com:USERNAME/REPOSITORY.git

you should set your user config for repositories where details differ from your global config details.

cd REPOSITORY

and modify git config

$ git config --local user.name "otherusername"

then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push

How to Move a Git Repository

Import your project to GitHub

 

 

mkdir -p ~/prestine/ #stores prestine git repos, no dev happens here (mostly just used for phpstorm framework intergartion)

move_repo(){
rm -fr ~/prestine/$2
git clone https://$1.git ~/prestine_repos/$2
cd ~/prestine_repos/$2
git remote set-url origin https://$2.git
git push origin master
cd -
}
# usage
move_repo github.com/username/reponame bitbucket.org/username/reponame

Multiple SSH Keys settings for different git account

Multiple SSH Keys settings for different git account
=================================================================

Create different public keys
———————————

Create different ssh keys for each git account

$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to tarranjones@github.com" -f ~/.ssh/tarranjones@github.com_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to otherusername@github.com" -f ~/.ssh/otherusername@github.com_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to tarranjones@bitbucket.org" -f ~/.ssh/tarranjones@bitbucket.org_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to otherusername@bitbucket.org" -f ~/.ssh/otherusername@bitbucket.org_id_rsa

If you would like to use a passphrase then remove `-N “”` from the command, you will then be prompted for you pass-phrase.

This will create the following files

~/.ssh/tarranjones@github.com_id_rsa
~/.ssh/otherusername@github.com_id_rsa
~/.ssh/tarranjones@bitbucket.org_id_rsa
~/.ssh/otherusername@bitbucket.org_id_rsa

Please refer to [github ssh issues](http://help.github.com/ssh-issues/) for common problems.

You can add your keys to memory (give to ssh-agent) using the `ssh-add` command,

$ ssh-add ~/.ssh/tarranjones@github.com_id_rsa
$ ssh-add ~/.ssh/tarranjones@bitbucket.org_id_rsa
$ ssh-add ~/.ssh/otherusername@github.com_id_rsa
$ ssh-add ~/.ssh/otherusername@bitbucket.org_id_rsa

or modify ~/.ssh/config to specify that keys should be automatically added to a running ssh-agent

To list your added keys

$ ssh-add -l

to delete all cached keys with

$ ssh-add -D

Modify the ssh config
———————————

Make sure ~/.ssh exists and is writable

$ mkdir -p $HOME/.ssh
$ chmod 0700 $HOME/.ssh

Make sure ~/.ssh/config exists and open in your text editor (subl)

$ cd ~/.ssh/
$ touch config
$ subl -a config

Then add the following

#Set Git User Domains
Host *-github.com *-bitbucket.org *-bitbucket.com
User git

#IdentityFile
Host tarranjones-*
IdentityFile ~/.ssh/tarranjones@%h_id_rsa

Host otherusername-*
IdentityFile ~/.ssh/otherusername@%h_id_rsa

#Hostnames
Host *-github.com
Hostname github.com

Host *-bitbucket.com *-bitbucket.org
Hostname bitbucket.org

Host *
Protocol 2
UseKeychain yes
AddKeysToAgent yes
IdentitiesOnly yes

Modify your Git config
———————–

I recommend setting your global config to match your most frequently used account details

$ git config --global user.name "tarranjones"
$ git config --global user.email "tarranjones@gmail.com"

Having the same username and email address for all your primary accounts across different git hosting providers is something worth thinking about.
Your user config would have to be changed a lot less.

Clone your repo and modify your Git config
———————————————

clone your repo

$ git clone tarranjones-github.com:username/repo.git

…or for an existing working directory

$ git remote set-url origin tarranjones-github.com:username/repo.git

you should set your user config for repositories where details differ from your global config details.

cd repo

and modify git config

$ git config --local user.name "otherusername"
$ git config --local user.email "otheremail@gmail.com"

then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push

Heres a little helper function

usage: ssh [user@]host]
function ssh_keygen(){
ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to $1" -f ~/.ssh/"$1_id_rsa"
}

$ ssh_keygen tarranjones@github.com

further reading
alias to switch user.email http://stackoverflow.com/a/33079036/2273611

Another related article in Chinese

1. http://4simple.github.com/docs/multipleSSHkeys/

ssh root@host – Permission denied, please try again.

This error usually occurs if PermitRootLogin is set to without-password meaning ssh keys should be used instead.
Currently i only have access to my server via the web console provided by my hosting provider.
To setup ssh you need to add your public key to
~/.ssh/authorized_keys.

Simple enough, but when the console doesn’t allow copy and paste it would be a rather tedious experience.

So by permitting root login with a password (temporarily) you can then access the server from your machine, add the ssh key (without having to type it manually) and then set PermitRootLogin back to without-password.

Once you’ve logged-in the fix is simple, edit the /etc/ssh/sshd_config file,
nano /etc/ssh/sshd_config

replace the line starting with PermitRootLogin(if it exists) with PermitRootLogin yes

and then for the changes to take affect you will need to restart the ssh service

sudo service ssh restart

After a few seconds go back to your own terminal and attempt to login again
ssh root@[your.ip.address.here]

You should be prompted for your password as usual but this time you shouldn’t receive an error.

So that fixed the problem but now anyone can login from anywhere (if the correct password is entered)

You should really use an ssh key, if your not sure if you have one heres some very useful articles from Github

If you are still logged in to the remote host type `exit` the enter the following command

cat ~/.ssh/id_rsa.pub | ssh root@[your.ip.address.here] "cat >> ~/.ssh/authorized_keys"

remember to replace your.ip.address.here with the remote host, You should be prompted for your password one last time.

If nothing seems to happen thats great.

Now attempt to login again
ssh root@[your.ip.address.here]

This time you shouldn’t be prompted for a password, now you have access from another machine you can change PermitRootLogin back to without-password.

Still in your own terminal edit the /etc/ssh/sshd_config file again,
nano /etc/ssh/sshd_config

an now replace PermitRootLogin yes with PermitRootLogin without-password

and

sudo service ssh restart
exit

then try logging in again ssh root@[your.ip.address.here]

You can check this has worked by trying to login via a machine without SSH setup.

Can I require a GitHub Gist using Composer?

Yes, and its pretty easy when you know how.
Composer is only aware of packages located in the default repository at located at packagist.org unless you tell it otherwise.

You can notify composer of other repositories and packages by editing the global or local composer.json file, either manually or via composers command line interface.

I have created a couple of shell functions which take advantage of composers cli which simplifies the process.



add_gist_repository(){

	composer config repositories.$1 '{"type":"package","package": {"name": "'$1'","version": "master","source": {"url": "https://gist.github.com/'$1'.git","type": "git","reference":"master"},"autoload": {"classmap": ["."]}}}'

}

require_gist(){
	add_gist_repository $1
	composer require $1:dev-master
}

require_dev_gist(){
	add_gist_repository $1
	composer require-dev $1:dev-master
}

Usage


$ require_gist tarranjones/33ac1e5de1663abeb6cabc0241bd3c96

Cast array keys to string

According to the PHP docs you can’t .

Strings containing valid integers will be cast to the integer type. E.g. the key “8” will actually be stored under 8. On the other hand “08” will not be cast, as it isn’t a valid decimal integer.

Depending on how you look at it theres either a bug or a feature which does allow you to do this and this is how its done.

$obj = new stdClass();
foreach($array as $key => $value){
    $obj->{$key} = $value;
}
$array = (array) $obj;

And heres a helper function


function array_change_key_type_string(array $array)
{
     $obj = new stdClass();
     foreach($array as $key => $value){
        $obj->{$key} = $value;
     }
     return (array) $obj;
}

Consistent broken Images across browsers

Broken images are a pain, there are lots you can do on the server to prevent this but very little you can do as a front end developer.
Caching assets or using data URIs are a couple of which may be available.

But it does happen and every browser displays a broken images differently. Missing attributes and incorrect values also have an affect on how they are displayed .
Here’s some examples of how they differ.

The DOM was Manipulated with JavaScript first to reduce the number of outputted broken image variations. After that the majority of the work was done using CSS.