<?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>micro tutorial &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/micro-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>PHP: Don&#8217;t Call the Destructor Explicitly</title>
		<link>/2011/11/14/php-dont-call-the-destructor-explicitly/</link>
		<comments>/2011/11/14/php-dont-call-the-destructor-explicitly/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 16:26:23 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[destructor]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php examples]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">/?p=2461</guid>
		<description><![CDATA[&#8220;PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++&#8221;[1] says the documentation for destructors, but let&#8217;s see the following class. class A { public function __construct() { echo 'building an object'; } public function __destruct() { echo 'destroying the object'; } } $obj = new A(); Well, as &#8230; <a href="/2011/11/14/php-dont-call-the-destructor-explicitly/" class="more-link">Continue reading <span class="screen-reader-text">PHP: Don&#8217;t Call the Destructor Explicitly</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" rel="bookmark" title="Object Cloning and Passing by Reference in PHP">Object Cloning and Passing by Reference in PHP </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><em>&#8220;PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++&#8221;</em><a href="http://www.php.net/manual/en/language.oop5.decon.php" title="PHP: Constructors and Destructors" target="_blank">[1]</a> says the documentation for destructors, but let&#8217;s see the following class.</p>
<pre lang="PHP">
class A
{
	public function __construct()
	{
		echo 'building an object';
	}
	
	public function __destruct()
	{
		echo 'destroying the object';
	}
}

$obj = new A();
</pre>
<p>Well, as you can not call the constructor explicitly:</p>
<pre lang="PHP">
$obj->__construct();
</pre>
<p>So we should not call the destructor explicitly:</p>
<pre lang="PHP">
$obj->__destruct();
</pre>
<p>The problem is that I&#8217;ve seen this many times, but it&#8217;s a pity that this won&#8217;t destroy the object and it is still a valid PHP code.<br />
<span id="more-2461"></span><br />
<figure id="attachment_2465" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2011/11/destruct.jpg"><img src="/wp-content/uploads/2011/11/destruct.jpg" alt="PHP destructors can&#039;t be called explicitly!" title="destruct" width="620" height="427" class="size-full wp-image-2465" /></a><figcaption class="wp-caption-text">PHP destructors cannot be called explicitely!</figcaption></figure></p>
<p>Constructors and destructors in <a href="/category/php/" title="PHP on stoimen.com">PHP</a> are part of the so called magic methods. Here&#8217;s what the <a href="http://php.net/manual/en/language.oop5.magic.php" title="PHP: Magic Methods" target="_blank">doc page</a> says about them.</p>
<blockquote><p>
The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state() and __clone() are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.</p></blockquote>
<p>To be more precise let&#8217;s take a look of the definition of destructors. </p>
<blockquote><p>PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.</p></blockquote>
<h2>What if I Call the Destructor Explicitly?</h2>
<p>Let&#8217;s see some examples!</p>
<pre lang="PHP">
class A
{
	public function __destruct()
	{
		echo 'destroying the object';
	}
}

$obj = new A();

// prints hello world
echo 'hello world';

// PHP interpreter stops the script execution and prints
// 'destroying the object'
</pre>
<p>This is actually a normal behavior. At the end of the script the interpreter frees the memory. Actually every object has a built-in destructor, just like it has built-in constructor. So even we don&#8217;t define it explicitly, the object has its destructor. Usually this destructor is executed at the end of the script, or whenever the object isn&#8217;t needed anymore. This can happen, for instance, at the end of a function body.</p>
<p>Now if we call the destructor explicitly, which as I said I&#8217;ve seen many times, here&#8217;s what happen.</p>
<pre lang="PHP">
class A
{
	public function __destruct()
	{
		echo 'destroying the object';
	}
}

$obj = new A();

// this is valid and it prints 'destroying the object'
// BUT IT DOES NOT DESTROY THE OBJECT
$obj->__destruct();

// prints hello world
echo 'hello world';

// HERE PHP ACTUALLY DESTROYS THE $obj OBJECT
// ... and again prints 'destroying the object'
</pre>
<p>As you can see calling the destructor explicitly doesn&#8217;t destroy the object. So the question is &#8230;</p>
<h2>How to Destroy an Object Before the Script Stops?</h2>
<p>Well, to destroy an object you can assign a NULL value to it.</p>
<pre lang="PHP">
class A
{
	public function __destruct()
	{
		echo 'destroying the object';
	}
}

$obj = new A();

// prints 'destroying the object'
$obj = null;

// prints 'hello world'
echo 'hello world';

// the script stop its execution
</pre>
<h2>Caution</h2>
<p>Be aware of that if you don&#8217;t clone the object $obj, and simply assign it to another variable, then $obj = null will be pointless. Let&#8217;s see the following example.</p>
<pre lang="PHP">
class A 
{
	public function printMsg()
	{
		echo 'I still exist';
	}
	
	public function __destruct()
	{
		echo 'destroying the object';
	}

}

$obj = new A();

// $newObj is pointing to $obj
$newObj = $obj;

// this doesn't destroy $newObj
// as it appears both $obj and $newObj point to the same memory
// so PHP doesn't free this memory
$obj = null;

// prints 'i still exist'
$newObj->printMsg();

// prints 'hello world'
echo 'hello world';

// now the scripts destroys the "object", which in this
// case is $newObj and prints 'destroying the object'
</pre>
<p>This example shows us that actually by assigning NULL to an object doesn&#8217;t quite destroy it if there are another objects pointing to the same memory.</p>
<pre lang="PHP">
class A 
{
	public function printMsg()
	{
		echo 'I still exist';
	}
	
	public function __destruct()
	{
		echo 'destroying the object';
	}

}

$b = new A();

$d = $c = $b;

$b = null;

$c->printMsg();
$d->printMsg();

// prints 'destroying the object' ONLY ONCE
</pre>
<p>In this last example there are two interesting things to note. First <strong>$b = null</strong> doesn&#8217;t call the destructor, and at the end of the script there&#8217;s only one implicit call of the destructor, although there are two objects.</p>
<h2>Conclusion</h2>
<p>The important thing to note is that you shouldn’t call the destructor of an object explicitly! Not because it will throw an fatal error, but simply because it won’t destroy the object.</p>
<p>[1] <a href="http://www.php.net/manual/en/language.oop5.decon.php" title="PHP: Constructors and Destructors" target="_blank">PHP: Constructors and Destructors</a></p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" rel="bookmark" title="Object Cloning and Passing by Reference in PHP">Object Cloning and Passing by Reference in PHP </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/11/14/php-dont-call-the-destructor-explicitly/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to Check if a Date is More or Less Than a Month Ago with PHP</title>
		<link>/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/</link>
		<comments>/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 14:45:52 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[date and time]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[strtotime]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unix timestamp]]></category>

		<guid isPermaLink="false">/?p=2418</guid>
		<description><![CDATA[Let&#8217;s say we have the following problem: we have to check whether a date is more than a month ago or less than a month ago. Many developers go in the wrong direction by calculating the current month and then subtracting the number of months from it. Of course, this approach is slow and full &#8230; <a href="/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/" class="more-link">Continue reading <span class="screen-reader-text">How to Check if a Date is More or Less Than a Month Ago with PHP</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2009/08/10/javascript-get-locale-month-with-full-name/" rel="bookmark" title="javascript get locale month with full name">javascript get locale month with full name </a></li>
<li><a href="/2012/03/16/you-think-you-know-php-quiz-results/" rel="bookmark" title="You think you know PHP. Quiz Results!">You think you know PHP. Quiz Results! </a></li>
<li><a href="/2011/10/19/thing-to-know-about-php-arrays/" rel="bookmark" title="Thing to Know About PHP Arrays">Thing to Know About PHP Arrays </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s say we have the following problem: we have to check whether a date is more than a month ago or less than a month ago. Many developers go in the wrong direction by calculating the current month and then subtracting the number of months from it. Of course, this approach is slow and full of risks of allowing bugs. Since, two months before January, which is the first month of the year, is actually November, which is the eleventh month. Because of these pitfalls, this approach is entirely wrong.<br />
<figure id="attachment_2442" style="width: 480px" class="wp-caption alignnone"><a href="/wp-content/uploads/2011/11/calendar.jpg"><img src="/wp-content/uploads/2011/11/calendar.jpg" alt="strtotime() is a lot more powerful than you think!" title="calendar" width="480" height="553" class="size-full wp-image-2442" srcset="/wp-content/uploads/2011/11/calendar.jpg 480w, /wp-content/uploads/2011/11/calendar-260x300.jpg 260w" sizes="(max-width: 480px) 100vw, 480px" /></a><figcaption class="wp-caption-text">strtotime() is a lot more powerful than you think!</figcaption></figure></p>
<p>The question is whether PHP cannot help us with built-in functions to perform these calculations for us. It is obvious, that from version 5.3.0 and later, there is an OOP section, which is great, but unfortunately this version is still not updated everywhere. So, how to accomplish the task?</p>
<h2>The Wrong Approach</h2>
<p>As I said, there are many ways to go in the wrong direction. One of them is to subtract 30 days from current date. This is completely wrong, because not every month has 30 days. Here, some developers will begin to predefine arrays to indicate the number of days in each month, which then will be used in their complicated calculations. Here is an example of this wrong approach.</p>
<pre lang="PHP">
echo date('Y-m-d', strtotime(date('Y-m-d')) - 60*60*24*30);
</pre>
<p>This line is full of mistakes. First of all <strong>strtotime(date(&#8216;Y-m-d&#8217;))</strong> can be replaced by the more elegant <strong>strtotime(&#8216;now&#8217;)</strong>, but for this later. Another big mistake is that <strong>60*60*24*30</strong>, which is <strong>number of seconds in 30 days</strong> can be predefined as a constant. Eventually the result is wrong, because not every month has 30 days.</p>
<h2>The Correct Approach</h2>
<p>A small research of the problem and the functions in versions prior of 5.3.0 of PHP is needed. Typical case study of date formatting happen when working with dates from a database. The following code is a classical example.<span id="more-2418"></span></p>
<pre lang="PHP">
// 2008 05 23, 2008-05-23 is stored into the DB
echo date('Y m d', strtotime('2008-05-23'));

// 2008 May 23
echo date('Y F d', strtotime('2008-05-23'));
</pre>
<p>The problem, perhaps, is that too often <a href="http://php.net/manual/en/function.strtotime.php" title="PHP: strtotime" target="_blank">strtotime()</a> is used like this, with exactly this type of strings. However much more interesting is that strtotime() can do much more. </p>
<h2>strtotime() Can Do Much More</h2>
<p>Let us first look at the documentation of this function. What parameters it accepts?</p>
<pre lang="PHP">
int strtotime ( string $time [, int $now = time() ] )
</pre>
<blockquote><p>The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.</p></blockquote>
<p>In particular we are interested in the first parameter, <em>time</em>.</p>
<blockquote><p><em>time </em>&#8211; A date/time string. Valid formats are explained in Date and Time Formats.</p></blockquote>
<p>It is especially important to note what are the <a href="http://www.php.net/manual/en/datetime.formats.php" title="PHP: Supported Date and Time Formats" target="_blank">valid Date and Time Formats</a>.</p>
<p>Here are the supported formats, but most interesting are those that are <a href="http://www.php.net/manual/en/datetime.formats.relative.php" title="PHP: Relative Formats" target="_blank">Relative</a>.</p>
<p>Exactly these formats are very convenient in our case, because they give us the ability to work with human readable strings, and here are some examples from the documentation of strtotime().</p>
<pre lang="PHP">
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
</pre>
<p>Thus, a valid string would be &#8220;1 month ago&#8221;.</p>
<pre lang="PHP">
// if current date is 2011-11-04, this will return 2011-10-04
echo date('Y-m-d', strtotime('1 month ago'))
</pre>
<p>Or &#8220;-1 month&#8221;:</p>
<pre lang="PHP">
// the same as the example above
echo date('Y-m-d', strtotime('-1 month'));
</pre>
<p>It&#8217;s interesting that &#8220;+1 -1 month&#8221; is also a valid string.</p>
<pre lang="PHP">
// 2011-10-04, if today's 2011-11-04
echo date('Y-m-d', strtotime('+1 -1 month'));
</pre>
<p>In fact strtotime() can do a lot more than most of the developers have ever imagined. Maybe its frequent use with string formatted dates (2010-01-13) makes it a bit unknown. Here are some interesting use cases.</p>
<pre lang="PHP">
// 1970-01-01, Calculations in braces are bad!
echo date('Y-m-d', strtotime('(60*60) minute'));

// 2 months into the future
echo date('Y-m-d', strtotime('-2 months ago'));
</pre>
<p>For instance, do you know how to get the date of the day before yesterday? Yes 2 days before today, but here&#8217;s yet another solution.</p>
<pre lang="PHP">
// 1 day before yesterday
echo date('Y-m-d', strtotime('yesterday -1 day'));
</pre>
<p>Another example is the fully human readable:</p>
<pre lang="PHP">
// get the first monday of the current month
echo date('Y-m-d', strtotime('first monday this month'));
</pre>
<h2>The Solution of the Task</h2>
<p>Finally, what is the solution of the original task? Well, just have to check whether a date is more or less than a month ago.</p>
<pre lang="PHP">
// a random date
$my_date = '2011-09-23';
	
// true if my_date is more than a month ago
(strtotime($my_date) < strtotime('1 month ago'))
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2009/08/10/javascript-get-locale-month-with-full-name/" rel="bookmark" title="javascript get locale month with full name">javascript get locale month with full name </a></li>
<li><a href="/2012/03/16/you-think-you-know-php-quiz-results/" rel="bookmark" title="You think you know PHP. Quiz Results!">You think you know PHP. Quiz Results! </a></li>
<li><a href="/2011/10/19/thing-to-know-about-php-arrays/" rel="bookmark" title="Thing to Know About PHP Arrays">Thing to Know About PHP Arrays </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Some Notes on the Object-oriented Model of PHP</title>
		<link>/2011/10/20/some-notes-on-the-object-oriented-model-of-php/</link>
		<comments>/2011/10/20/some-notes-on-the-object-oriented-model-of-php/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 15:08:15 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[abstract classes]]></category>
		<category><![CDATA[Abstract type]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[experiment]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[iheritance]]></category>
		<category><![CDATA[Interfaces]]></category>
		<category><![CDATA[Java programming language]]></category>
		<category><![CDATA[Method]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[php 5]]></category>
		<category><![CDATA[php class definition]]></category>
		<category><![CDATA[php experiment]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[Polymorphism in object-oriented programming]]></category>
		<category><![CDATA[Software design patterns]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Virtual function]]></category>

		<guid isPermaLink="false">/?p=2401</guid>
		<description><![CDATA[PHP 5 introduces interfaces and abstract classes. To become a little clearer, let us see their definitions. Interfaces Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. Interfaces are defined using the interface keyword, in the same way as a &#8230; <a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" class="more-link">Continue reading <span class="screen-reader-text">Some Notes on the Object-oriented Model of PHP</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/05/30/object-oriented-javascript-inheritance/" rel="bookmark" title="Object Oriented JavaScript: Inheritance">Object Oriented JavaScript: Inheritance </a></li>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
<li><a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" rel="bookmark" title="Object Cloning and Passing by Reference in PHP">Object Cloning and Passing by Reference in PHP </a></li>
<li><a href="/2011/11/14/php-dont-call-the-destructor-explicitly/" rel="bookmark" title="PHP: Don&#8217;t Call the Destructor Explicitly">PHP: Don&#8217;t Call the Destructor Explicitly </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>PHP 5 introduces interfaces and abstract classes. To become a little clearer, let us see their definitions.</p>
<h2>Interfaces</h2>
<blockquote><p>Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.<br />
Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.<br />
All methods declared in an interface must be public, this is the nature of an interface. </p>
<p>To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma. </p></blockquote>
<h2>Abstract Classes</h2>
<blockquote><p>PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method&#8217;s signature &#8211; they cannot define the implementation.</p>
<p>When inheriting from an abstract class, all methods marked abstract in the parent&#8217;s class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. This also applies to constructors as of PHP 5.4. Before 5.4 constructor signatures could differ. </p></blockquote>
<h2>Some Cases</h2>
<p>Now let&#8217;s do a quick experiment. According to the definition of interfaces we can define an interface and than an abstract class can implement this interface.<br />
<span id="more-2401"></span></p>
<pre lang="PHP">
interface A
{
	public function a();
}

abstract class B implements A
{
	public function a()
	{
		return 10;
	}
}
</pre>
<p>However the abstract class cannot be instantiated so we need to extend it.</p>
<pre lang="PHP">
interface A
{
	public function a();
}

abstract class B implements A
{
	public function a()
	{
		return 10;
	}
}

class C extends B
{
	...
}
</pre>
<p>In the first place is completely OK to define an abstract class that does not contain any abstract method. TUsually if a class has one or more abstract methods <em>(Methods defined as abstract simply declare the method&#8217;s signature &#8211; they cannot define the implementation, according to the docs)</em>, it must be defined as abstract. But we can also define an abstract class which has no abstract methods.</p>
<pre lang="PHP">
abstract class B
{
	public function a()
	{
		return 5;
	}
}

class C extends B
{
	public function a()
	{
		return 10;
	}
}

$c = new C();
echo $c->a();
</pre>
<p>OK then what would happen if we had an interface that is implemented by an abstract class, which in turn is extended by a given class. First of all if a class implements an interface, even if it is an abstract class, it must implement all methods defined in the interface. So if the predefined method in the abstract class is marked as abstract, it would result into a fatal error.</p>
<pre lang="PHP">
interface A
{
	public function a();
}

abstract class B implements A
{
	abstract public function a();
}

class C extends B
{
	public function a()
	{
		return 5;
	}
}

$c = new C();
echo $c->a();
</pre>
<p>This is perfectly logical, because in fact each method of the interface must be implemented by his children.</p>
<p><em>&#8220;All methods in the interface must be implemented within a class; failure to do so will result in a fatal error.&#8221;<br />
</em><br />
Fine, but we can have abstract classes with no abstract methods, let&#8217;s try this:</p>
<pre lang="PHP">
interface A
{
	public function a();
}

abstract class B implements A
{
	public function a()
	{
		return 10;
	}
}

class C extends B
{
	public function a()
	{
		return 5;
	}
}

$c = new C();
echo $c->a();
</pre>
<p>This code is working and the result is the returned value from C::a(). However class B is abstract so let&#8217;s go even further. Let&#8217;s define an abstract method, different from the method B::a().</p>
<pre lang="PHP">
interface A
{
	public function a();
}

abstract class B implements A
{
	public function a()
	{
		return 10;
	}
	
	abstract public function b();
}

class C extends B
{
	public function a()
	{
		return 5;
	}
	
	public function b()
	{
		return "hello world!";
	}
}

$c = new C();
echo $c->a();
echo $c->b();
</pre>
<p>Now there&#8217;s a fatal error. Why?</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/05/30/object-oriented-javascript-inheritance/" rel="bookmark" title="Object Oriented JavaScript: Inheritance">Object Oriented JavaScript: Inheritance </a></li>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
<li><a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" rel="bookmark" title="Object Cloning and Passing by Reference in PHP">Object Cloning and Passing by Reference in PHP </a></li>
<li><a href="/2011/11/14/php-dont-call-the-destructor-explicitly/" rel="bookmark" title="PHP: Don&#8217;t Call the Destructor Explicitly">PHP: Don&#8217;t Call the Destructor Explicitly </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/20/some-notes-on-the-object-oriented-model-of-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Thing to Know About PHP Arrays</title>
		<link>/2011/10/19/thing-to-know-about-php-arrays/</link>
		<comments>/2011/10/19/thing-to-know-about-php-arrays/#respond</comments>
		<pubDate>Wed, 19 Oct 2011 15:18:47 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[C programming language]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Foreach]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[interpreter]]></category>
		<category><![CDATA[PHP arrays]]></category>
		<category><![CDATA[PHP micro tutorial]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">/?p=2390</guid>
		<description><![CDATA[Consider the following case. We have an array with identical keys. $arr = array(1 => 10, 1 => 11); What happens when the interpreter reaches this line of code? This is not a syntax error and it is completely valid. Very similar, but more interesting case is when we have an array of identical keys, &#8230; <a href="/2011/10/19/thing-to-know-about-php-arrays/" class="more-link">Continue reading <span class="screen-reader-text">Thing to Know About PHP Arrays</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" rel="bookmark" title="PHP: What is More Powerful Than list() &#8211; Perhaps extract()">PHP: What is More Powerful Than list() &#8211; Perhaps extract() </a></li>
<li><a href="/2012/07/24/php-arrays-or-linked-lists/" rel="bookmark" title="PHP: Arrays or Linked Lists?">PHP: Arrays or Linked Lists? </a></li>
<li><a href="/2010/05/19/php-associative-arrays-coding-style/" rel="bookmark" title="PHP Associative Arrays Coding Style">PHP Associative Arrays Coding Style </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Consider the following case. We have an array with identical keys. </p>
<pre lang="PHP">
$arr = array(1 => 10, 1 => 11);
</pre>
<p>What happens when the interpreter reaches this line of code? This is not a syntax error and it is completely valid. Very similar, but more interesting case is when we have an array of identical keys, where those identical keys are represented once as an integer and then as a string.<br />
<figure id="attachment_2404" style="width: 640px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/10/php.code_.jpg"><img src="/wp-content/uploads/2011/10/php.code_.jpg" alt="Keys in PHP arrays are not type sensitive, so pay attention when using them!" title="PHP Code" width="640" height="480" class="size-full wp-image-2404" srcset="/wp-content/uploads/2011/10/php.code_.jpg 640w, /wp-content/uploads/2011/10/php.code_-300x225.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" /></a><figcaption class="wp-caption-text">Keys in PHP arrays are not type sensitive, so pay attention when using them!</figcaption></figure></p>
<pre lang="PHP">
$arr = array(1 => 10, "1" => 11);
</pre>
<p>Now several questions arise. First of all, how many elements have this array? Two or one. This can be easily verified by checking what count() will return.<span id="more-2390"></span></p>
<pre lang="PHP">
echo count($arr);
</pre>
<p>The correct answer is 1. This simply means, that there&#8217;s no difference between string keys and integer keys. What would happen if we had a &#8220;normal&#8221; array with different keys?</p>
<pre lang="PHP">
$arr = array(1 => 10, "2" => 11);
echo count($arr);
</pre>
<p>As expected this returns 2. </p>
<p>Next thing to check is what&#8217;s in the array after this initialization line.</p>
<pre lang="PHP">
$arr = array(1 => 10, "1" => 11);
</pre>
<p>Is there something in the first element $arr[0], or there&#8217;s something in the second element $arr[1]? What is the value of the single value?<br />
As it appears the second element replaces the first one. We&#8217;ve seen that the array has only one value, but where&#8217;s that value? The only way to check this is to dump both elements:</p>
<pre lang="PHP">
var_dump($arr);
</pre>
<p>Here we can see that $arr[1] contains &#8220;11&#8221; and it is the only value, and $arr[0] is not set.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" rel="bookmark" title="PHP: What is More Powerful Than list() &#8211; Perhaps extract()">PHP: What is More Powerful Than list() &#8211; Perhaps extract() </a></li>
<li><a href="/2012/07/24/php-arrays-or-linked-lists/" rel="bookmark" title="PHP: Arrays or Linked Lists?">PHP: Arrays or Linked Lists? </a></li>
<li><a href="/2010/05/19/php-associative-arrays-coding-style/" rel="bookmark" title="PHP Associative Arrays Coding Style">PHP Associative Arrays Coding Style </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/19/thing-to-know-about-php-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
