<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Swedish fika &#187; PHP</title>
	<atom:link href="http://www.swedishfika.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.swedishfika.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 25 Jul 2010 21:57:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Escaping from Encoding Hell</title>
		<link>http://www.swedishfika.com/2010/01/19/escaping-from-encoding-hell/</link>
		<comments>http://www.swedishfika.com/2010/01/19/escaping-from-encoding-hell/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 20:33:57 +0000</pubDate>
		<dc:creator>Ida</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://swedishfika.com/?p=792</guid>
		<description><![CDATA[What we gonna talk about here is character encoding and how to convert from ISO-8859-1 to UTF-8. If this doesn&#8217;t seem familiar to you you might want to start learning a bit about it. Cause hell is a scary place and you don&#8217;t want to end up there. This is how to avoid hell and [...]]]></description>
			<content:encoded><![CDATA[<p>What we gonna talk about here is character encoding and how to convert from <a href="http://en.wikipedia.org/wiki/ISO/IEC_8859-1">ISO-8859-1</a> to <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. If this doesn&#8217;t seem familiar to you you might want to start learning a bit about it. Cause hell is a scary place and you don&#8217;t want to end up there. This is how to avoid hell and for those of you who are struggling in the darkness; this is how to get your ass out of there.<br />
<span id="more-792"></span></p>
<h3>Why would I need to convert?</h3>
<ul>
<li>You are tired of using entities for foreign characters. Hate using things like this &amp;#373; (&#8592; very common character… uhm… ) </li>
<li>You are going to merge your web site with another website. (Which has a different encoding.)</li>
<li>You want to write in more languages. (I&#8217;m talking about languages as in the languages people speak NOT programming languages.) </li>
<li>When generating emails and sending data you have problems with weird signs. </li>
<li>You are mad because ISO-8859-1 doesn&#8217;t contain an euro sign (€). (Isn’t that quite funny? I mean it&#8217;s encoding especially suited for the west of europe.  Yea, yea &amp;euro; I know. But still.) </li>
</ul>
<p>There are more reasons. Feel free to add those in the comments.</p>
<p>The biggest reasons <em>&ldquo;back in the days&rdquo;</em> to use ISO-8859-1 was that it had slightly better support in older user agents. Well those old agents are pretty much gone by now. Who still uses Netscape 4? Hands up! Anyone? Well, that’s what I thought.<br />
All in all UTF-8 is, in the long run, more fit for the future. </p>
<h3>Don’t go to hell</h3>
<p>So what do I mean by &ldquo;Encoding Hell&rdquo;. Well those who has been there know what I mean. It’s when you just switch encoding without properly converting your data and you end up with letters looking sÃ¥ hÃ¤r (that’s the word &ldquo;så här&rdquo; meaning &ldquo;like this&rdquo; in swedish.) I got this nasty letters by opening a file with UTF-8 encoding in my text editor using ISO-8859-1.  And guess what? Opening with the right encoding doesn’t switch the letters back. They are stuck looking nasty. I’m in hell! </p>
<p><img src="http://swedishfika.com/wp-content/uploads/in_hell.jpg" alt="Letters in Encoding Hell" title="In Hell" width="456" height="85" class="alignnone size-full wp-image-802" /></p>
<p>Well this was only one file and sure you can change the letters manually or by using some fancy search/replace-command. But think about if you made this mistake with hundreds or thousands of files? Not nice. Big hell!</p>
<h3>Do it right from the beginning</h3>
<p>The best thing is to avoid Encoding Hell all together. Choose the right encoding from start and you are safe.<br />
But if you must change. First thing. Be careful! If you are careful you will save time. Make up a converting-encoding-plan  first so you know exactly what you are doing. Just don’t start testing. My advice is to always start with a backup. Backup everything you want to convert. If you have a database connected to your webpages don’t forget to back up that one as well.  </p>
<h3>Converting your files</h3>
<p>Don’t even think of converting your files by using your text editor. It usually turns out nasty. There are tools to help us out with this.  You can use the terminal/the command prompt to convert things. Me, myself, I have tried out <a href="http://www.elfdata.com/encodingmaster/">&ldquo;Encoding Master&rdquo;</a>  which I think is simply brilliant. Easy to understand and it converted everything very nicely. It&#8217;s also supported by both PC and Mac.<br />
Don’t forget to add/change the encoding in the headers of the webpages too.<br />
<code>&lt;meta http-equiv="Content-Type" content="text/html;<br />
charset=utf-8"&gt;</code></p>
<h3>Converting your database</h3>
<p>Be careful here. Don’t start adding UTF-8 encoded data into your old database. Make sure it’s converted and pretty first. Otherwise you might end up with a messy mix and bang! You are back in hell!<br />
To change the encoding of your database you must change the encoding of everything. The database itself. Every table in the database and every column that includes some sort of text-value. I’m talking about data of type: char, varchar, tinytext, text, mediumtext and longtext.<br />
Since I lately been working with PHP and MySQL the following will apply to them. If you are working in other languages and other databases you probably need to do some further research to see what is the right methods to use. </p>
<p>To change the databse itself use the following query:<br />
<code>ALTER DATABASE my_database charset=utf8;</code><br />
To change a table use :<br />
<code>ALTER TABLE my_table charset=utf8;</code><br />
To change a column in a table use:<br />
<code>ALTER TABLE my_table MODIFY my_column TEXT CHARACTER SET utf8;</code></p>
<p>Make sure everything in there uses the character set utf8 and <a href="http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html">collation utf8_unicode_ci</a>. Ok? </p>
<h3>Encoding in PHP</h3>
<p>If you are coding in PHP you might have problems with the MySQL and PHP talking UTF-8 language. Solution to this which worked for me was to send the following query:<br />
<code>SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'; </code> This must be done right after connecting to the database. </p>
<p>From <a href="http://simonkjellberg.com">Simon</a> comes this handy tip to those of you who are using MySQLI together with UTF-8:<br />
(With error-handling included as a bonus…)</p>
<p><code>$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, <br />DB_DATABASE);<br />
if(mysqli_connect_error()){<br />
    throw new Exception('Connection failed<br /> due to the following error: '.$error);<br />
}<br />
   if (!$mysqli->set_charset("utf8")){<br />
   printf("Error loading character set utf8: %s\n", <br />$mysqli->error);<br />
} </code> </p>
<p>It could also be a good idea to add the code below to the beginning of the PHP-files.<br />
<code>mb_language('uni');<br />
mb_internal_encoding('UTF-8');</code></p>
<h3>Last but not Least…</h3>
<p>Always research carefully before changing your encoding. Especially if you are a converting-virgin and are changing the encoding for the first time. Never rely solely on one tutorial. Not even this one which, I need to admit, is not covering everything. This is pretty general info. Hey, you can write a book about encoding and the struggles with converting. So remember to look around. Ask people. Start with a backup. Always. And, again, be careful! </p>
<p>Happy Encoding!</p>
<p>/Ida</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swedishfika.com/2010/01/19/escaping-from-encoding-hell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Overloading constructors in PHP</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/</link>
		<comments>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 07:44:35 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://swedishfika.com/?p=71</guid>
		<description><![CDATA[As far as I know, there is not an easy way to overload a constructor in PHP in the same way as in C# or Java. There seems to be a whole bunch of different solutions or workarounds regarding this issue. I&#8217;ve been working a lot with PHP in my degree project and this is [...]]]></description>
			<content:encoded><![CDATA[<p>As far as I know, there is not an easy way to overload a constructor in PHP in the same way as in C# or Java. There seems to be a whole bunch of different solutions or workarounds regarding this issue. I&#8217;ve been working a lot with PHP in my degree project and this is the technique we used to &#8220;overload&#8221; constructors.<span id="more-71"></span></p>
<p>You can create an array that contains your arguments and then check which parameters that are set in the array in the constructor. First you check the length of the parameter list and then which parameters that are declared. Then you set the class instance variables to the corresponding parameter value. </p>
<p><code>class Something<br />
{<br />
  private $a;<br />
  private function SetA($a)<br />
  {<br />
    $this-&gt;a = $args['a'];<br />
  }<br />
  function __construct($args)<br />
  {<br />
    $numArgs = sizeof($args);<br />
    switch ($numArgs)<br />
    {<br />
      case 1:<br />
      if(isset($args['a']))<br />
      {<br />
        $this-&gt;SetA($args['a']);<br />
      }<br />
      break;<br />
    }<br />
  }<br />
}</code></p>
<p>In whatever file that you need to create an object of the class above you can write the following code:</p>
<p><code>$args = array('a' =&gt; 'someValue');<br />
$s = new Something($args);</code></p>
<p>The thing that I like the most with this technique with using an array in PHP is that you will most unlikely run out of a possibility to set whatever you want to set in your constructor. When you&#8217;re programming with C#, for example, it&#8217;s pretty easy to run out of constructors. Have you once declared a constructor that takes one string parameter you cannot overload that constructor with another constructor that also takes one string parameter. </p>
<p>Is there anyone who have another idea/tip on how to overload constructors using PHP?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
