PHP: casting stdClass Object to a array

When using Soapclient to access a webservice the returned object will be of a stdClass Object.

If you don’t wish to access this as an object, but would find it easier to use it as an array then a quick cast solves this.

/*As an object*/
print_r($soapResult->contents);
/*cast to an array*/
$soapResult = (array)$soapResult;
print_r($soapResult['contents']);

Turning off password saving (on a form)

If you have a form that you do not wish to use autocompletion for (either you want to disable password auto completion “for real”, or perhaps you have a form where the saved credentials are filled in where they shouldn’t be) then a quick tip to achieve this is to set ‘ autocomplete=”off” ‘ on the form.

<form id="loginForm" action="login.php" method="post" autocomplete="off">

svnadmin dump vs hotcopy

  • hotcopy makes a direct copy of the repository. It is fast, but it keeps information about the underlying file system etc. The server where the copy is imported needs to be the same version, have the same config etc.
  • dump is version independent. This means that the exported copy can be used by any version of svn.

IMHO: dump is better for backups and long term archiving (outside a repository) [like a tape in a vault].
hotcopy is better suited to make a quick copy to another server when the other server is up and running.

The base information for this post was found in the svn maillist

Linux: Converting a file encoded in ISO-8859-1 to UTF-8

If you have a file that is saves as ISO-8859-1 (or ISO-LATIN-1 if you like to call it that) and wish to convert it to UTF-8 you can use:

 iconv --from-code=ISO-8859-1 --to-code=UTF-8 ./oldfile.htm > ./newfile.html

This will create a new file with the converted encoding.

iconv can of of course convert to and from several other charsets. To see a list of all the encodings that iconv can work with use:

 iconv -l

If you wish to massconvert files find can be used with exec

 find . -name "*.txt" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o {}.utf8 \;

svn: Checksum mismatch for file expected: md5, actual: othermd5

What has (most likely) happened is that the .svn/entries file has gotten corrupt somehow; this can happen if a svn client dies halfway in a commit, or for other reasons, but that is not what this post is about.

Recently got the error message “svn: Checksum mismatch for ‘/var/www/magento/app/locale/sv_SE/template/email/sales/.svn/text-base/testfile.svn-base’; expected: ‘2b991059e896f837f33875df8070e1ab’, actual: ’71d7d03cf41d65b6c28471dd2e96c2e1′”

A quick fix this issue is to

  • Remove the .svn directory from the sales folder.
  • Move the folder sales to sales_real
  • Update the email folder (this will recreate the sales folder)
  • Move all the files from sales_real to sales
  • Commit sales
  • Delete sales_real

Obligatory disclaimer: This worked for me, it keeps the file history and all should be well.
If the sales folder would have been larger (and or my connection to the svn server slower) perhaps some other solution is preferred; but this was quick and worked.

Credit to dr. chuck for the original post

PHP5: Quick on exceptions

Very quick (&dirty):

1
2
3
4
5
6
7
8
try{
	doStuff();
}
catch (Exception $e){
	echo 'Error on line '. $e->getLine().' in '. $e->getFile() . $e->getMessage();
	//Or if you are working inside an object, you could use.
	echo 'Error on line '.$this->getLine().' in '.$this->getFile() . $e->getMessage();
}

Now to catch a specific kind of exception (in this case CustomException), and say woups, but to re-throw all other Exceptions.

1
2
3
4
5
6
7
8
9
10
11
try{
	doStuffThatCanGiveCustomException();
}
catch (Exception $e){
	//echo get_class($e) . "<br/>";
	if (get_class($e) == "CustomException" && $e->getMessage() === "My custom exception message."){
		echo "woups";
	}
	else
		throw $e;
}

To throw a custom exception:

 throw new Exception('My exception message');

For more informaion about this please check out the php manual

500 internal sever error after moving CakePHP

If the CakePHP is run in production mode (debug =0), then it is not possible to move the CakePHP to another folder – after the move the site will simply give an error 500. This is since a number of things are cached in CackePHP and if the app is in production mode, then some of the cache is not checked if it needs to be updated.

So if you intend to move a CakePHP directory that is setup as a production environment, then you will need to turn up debug to at least 1 , load the site – then it can be turned back down again.

IIS/ASP.NET turning (browser) debug messages off

To make this change for one project at a time:

In  “Web.config”find the line saying “compilation debug=true” and switch this to false.

To make this change for the entire server:

In “%SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\Machine.config” find the line saying “compilation debug=true” and switch this to false.

More info on this from Microsoft:http://support.microsoft.com/kb/815157