<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>PHPDeveloper.org</title>
    <link>http://www.phpdeveloper.org</link>
    <description>Up-to-the Minute PHP News, views and community</description>
    <language>en-us</language>
    <pubDate>Sun, 12 Oct 2008 13:35:28 -0500</pubDate>
    <ttl>30</ttl>
    <item>
      <title><![CDATA[Community News: Namespaces in PHP6]]></title>
      <guid>http://www.phpdeveloper.org/news/8194</guid>
      <link>http://www.phpdeveloper.org/news/8194</link>
      <description><![CDATA[<p>
Spurred on by a <a href="http://schlitt.info/applications/blog/exit.php?url_id=3868&entry_id=556">recent post</a> to the php-dev mailing list by <i>Dmitry Stogov</i> the ever-popular "namespaces discussion" has surged back to the top of everyone's list. <i>Dmitry</i> proposed <a href="http://dev.daylessday.org/diff/tests.tar.gz">a patch</a> for PHP6 that would easily implement namespaces for the upcoming version.
</p>
<blockquote>
All class and function names inside are automatically prefixed with namespace name. Inside namespace, local name always takes precedence over global name. It is possible to use the same namespace in several PHP files. The namespace declaration statement must be the very first statement in
file.
</blockquote>
<p>
The general idea is to provide an easy separation of functionality but to keep things accessible at any level (in scope, in parent scope, etc). He's <a href="http://marc.info/?l=php-dev&m=118355320225178&w=2">included code</a> in his mailing list post to illustrate how it would all work.
</p>
<p>
Several others in the PHP community have jumped on and are already talking about this new proposal:
</p>
<ul>
<li><a href="http://www.rooftopsolutions.nl/article/139">Evert Pot's post</a>
<li>a <a href="http://www.phpguru.org/#127">blog entry</a> from <i>Richard Heyes</i> proposing a namespace character
<li><a href="http://php100.wordpress.com/2007/07/05/namespaces-can-we-keep-it-simple/">a new post</a> from the PHP 10.0 Blog on simplicity in namespaces
<li><a href="http://www.tonybibbs.com/article.php/NamespacesInPHP6">this new post</a> from <i>Tony Bibbs</i>
<li><a href="http://schlitt.info/applications/blog/index.php?/archives/556-I-love-namespaces.html">some thoughts from</a> <i>Tobias Schlitt</i> happily supporting the proposal
<li><a href="http://www.dynamicwebpages.de/99.rdfnews.php?select=1170">this post</a> from DynamicWebPages.de
</ul>]]></description>
      <pubDate>Fri, 06 Jul 2007 11:10:00 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[APress' Inside Open Source Blog: Namespaces in PHP 6]]></title>
      <guid>http://www.phpdeveloper.org/news/6726</guid>
      <link>http://www.phpdeveloper.org/news/6726</link>
      <description><![CDATA[<p>
As <a href="http://opensource.apress.com/article/193/namespaces-in-php-6">this post</a> over on APress' Inside Open Source blog today, there's a discussion that's been going on over on the php-dev mailing list <a href="http://groups.google.com/group/mailing.www.php-dev/browse_thread/thread/eeb773e2acbd72e5/c7c51fdbc6d892e6?q=namespaces&lnk=ol&hl=en&">about namespaces in PHP6</a>.
</p>
<blockquote>
I find both sides of the argument quite compelling, and while FWIW I'm in favor of including them in the next release, I'm somewhat indifferent to the matter. According to one of the very latest messages in the thread, Zeev Suraski confirms they simplistic support will likely be enabled.
</blockquote>
<p>
That's <i>Jason Gilmore</i> talking there, and he goes on to reinforce something <i>Rasmus Lerdorf</i> has said about namespaces in the past - they'll implement them when they find the right way (in the PHP spirit) to do it.
</p>]]></description>
      <pubDate>Fri, 17 Nov 2006 07:30:53 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Tutorial: An Introduction to OOP in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/5719</guid>
      <link>http://www.phpdeveloper.org/news/5719</link>
      <description><![CDATA[<p>
Okay, show of hands out there - who else is tired of the boring old car analogies when it comes to talking about object-oriented programming in PHP? I have to admit; even I got a little sick of reading them after a bit. It seemed like there wasn't much originality behind them, and several of them just assumed that you understood what "$this->car_name" was.
</p>
<p>
So, here we go with something a little bit different - hopefully it'll turn out to be something useful for all of you developers out there trying to wade through the wide world of object-oriented programming with PHP.
</p>
<p>
<a href="http://www.phpdeveloper.org/news/5719">Read on for the rest of the tutorial!</a>
</p>
<split/>
<p>
<b>What's so cool about OOP anyway?</b><br/>
Well, let's start at the beginning and lay down some basic terms to get things started. First off, most PHP developers write when their first coming into the language is called "procedural". In this kind of code, one thing happens after another and the most exciting thing that could happen is the inclusion of another file. Functions are scattered here and there, and a library of them may even be used to help move things along a bit faster. There are limitations to this kind of programming, though. Procedural code tends to get pretty unwieldy pretty quickly when working with larger site. Shifting things around to included files helps, but it doesn't get to the root of the problem.
</p>
<p>
Object-oriented programming can help with all of this. Let's back up just a second and talk about what the "object" part in that term means. Objects are variable types in PHP that can do all sorts of fun things. They can be passed around from place to place all while maintaining all of their properties and methods inside. If that doesn't make sense to you yet, hang with me a bit, I'm going to help. The first step is to discover how to create an object of your very own.
</p>
<p>
<b>The Object of Our Affections</b><br/>
Objects are really just a fancy variable type (as mentioned above) with some nifty features that come along with them. To create an object, you'll need something called a class. Think of a class as the framework behind an object that defined the functionality and variables to keep inside. Here's an example:
</p>
<p>
[php]
<?php
class myClass {
	function myClass(){	
	}
}
?>
[/php]
</p>
<p>
In this case, "myClass" is, well, the name of the class and the word "class" is a keyword that PHP looks at to know what you're doing. Right inside the class definition, there's a function, "myClass". Now, I'm not trying to confuse you on purpose - there's a reason why it's named the same as the class. It's a special kind of function called the constructor. Just like laying the foundation and putting up the walls of a new building is part of constructing it, our "myClass" function is run when the object is first created. 
</p>
<p>
Ah! Now to the fun part! Object creation! (Well, not so much fun as the next step in the process).
</p>
<p>
So, we have our little sample class above sitting there, ready to use. But, to be able to get to anything inside it, we need to make an object that represents it. Here's an example:
</p>
<p>
[php]
<?php
$mine=new myClass();
?>
[/php]
</p>
<p>
Man that's hard work...well, okay - so it's not, but there's still a little there to explain. The variable "$mine" is the newborn object. If you do a print_r() on it (you do know about print_r, don't you?), you can see that it's a little bit special. To tell PHP that we want to make it an object, we use the "new" keyword along with the name of the class. Our example above doesn't really do anything yet, but that's about to change when we introduce properties.
</p>
<p>
<b>Putting Properties Into the Mix</b><br/>
Like everything else involved with classes, properties are really just something you already know (and love). Properties are just variables in disguise. Why are they different? Well, Inside of a class, you can have variables, but there are some variables that ascribe to a higher calling. These variables can be accessed both inside and outside of the class and are global inside an object. Want an example? Here's a simple one:
</p>
<p>
[php]
<?php
class myClass {

	var $my_var = 'testing';

	function myClass(){
	}
}
$mine=new myClass();
echo $mine->my_var;
?>
[/php]
</p>
<p>
In our example above, we're using the same class structure as before, but we've added something different. The "$my_var" variable defined at the top puts that value out where we can get to it later. Later comes around when, after creating the object, we have some fun with a new operator entering the game, the "->" (or as I usually call it, the arrow). Basically, this tells PHP that the variable you're referencing is a part of the "$mine" object. PHP automatically pulls the current value from the object and echoes it out just like any other value. And this isn't limited to just variables, either - you can use the arrow to call methods too (functions in a class, remember), like so:
</p>
<p>
[php]
<?php
class myClass {
	function myClass(){
	}
	function echoMe(){
		echo 'me';
	}
}
$mine=new myClass();
$mine->echoMe();
?>
[/php]
</p>
<p>
Running this script will result in the text "me" being output to the page. Pretty simple, huh?
</p>
<p>
<b>Taking the Next Step</b><br/>
Since we're starting to get a bit more complex anyway, why don't we apply all of this great knowledge that we've already accumulated into something a bit more useful - like a simple graphical example. In this example we're going to create a box on the page (a DIV tag) and with the help of PHP and some CSS, change a few things about it. But first, the code:
</p>
<p>
[php]
<?php
class myHappyBox {

	var $box_height = 100;
	var $box_width = 100;
	var $box_color = '#EC0000';

	function myHappyBox(){
	}

	function setHeight($value){
		$this->box_height=$value;
	}
	function setWidth($value){
		$this->box_width=$value;
	}
	function setColor($value){
		$this->box_color=$value;
	}

	function displayBox(){
		echo sprintf('
			<div style="height:%spx;width:%spx;background-color:%s"> </div>
		',$this->box_height,$this->box_width,$this->box_color);
	}
}
$box=new myHappyBox();
$box->displayBox();
?>
[/php]
</p>
<p>
Now, don't let this code scare you off, it's really not that complex. There's a few new things to look at in here, but nothing that doesn't make sense with the right explanation. We know that we're going to be building a box with this class, so we know that the box is going to have certain properties - height, width, and a color. Looking at the class above, you can see those three settings defined as properties with default values. By defining them there at the top, we have settings to fall back on if we do like the example shows and just call displayBox(). As it stands, the example will show a 100 pixel by 100 pixel DIV with a red background. This is all well and good, but what happens if we want to change one of these settings? Well, that's what the other functions are for.
</p>
<p>
There are three "set" functions in our myHappyBox class - one for height, one for width, and one for the background color - and they all work the same basic way. Each of them takes in a value and sets the global (to the object) property to the new value. So, if we wanted to change the size of the box, we could do:
</p>
<p>
[php]
<?php
$box=new myHappyBox();
$box->setHeight(30);
$box->setWidth(300);
$box->displayBox();
?>
[/php]
</p>
<p>
The above code would then alter our default box and display one that's a whole lot wider than it is tall. The same kind of change can be made with the setColor method, giving it a value of an HTML color (hex).
</p>
<p>
<b>Wrapping it all up</b><br/>
So, that's basically it - not really anything to be scared of or to avoid for the future. In fact, object-oriented programming can be one of the better things to happen to you and your code. Sure, rewriting that application you're currently on in a more "OOP fashion" might be a big pain, but there's always the next project to consider. And remember, there's more to working with objects, classes, methods, properties, blah, blah, blah than what I've said here. You should definitely head over to the PHP.net site and check out <a href="http://www.php.net/oop">their great resource</a> on the topic. It has the answers to the questions you'll want to know now that you're finished with this little tutorial.
</p>]]></description>
      <pubDate>Wed, 28 Jun 2006 12:14:05 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Tutorial: A Simple Sessions Tutorial]]></title>
      <guid>http://www.phpdeveloper.org/news/5704</guid>
      <link>http://www.phpdeveloper.org/news/5704</link>
      <description><![CDATA[<p>
Developers just starting out with PHP have a pretty easy time getting the basics down, but there are a few things that can take a little time to get ones head around. I just looking around the PHP community, I've noticed a bit of a barrier when it comes to getting aquainted with sessions. So, to help overcome this bump in the road, here's a brief introduction to sessions - what they are and how they can help you.
</p>
<p>
<a href="http://www.phpdeveloper.org/news/5704">Click here for the full article</a>!
</p>
<split/>
<p>
<b>What are sessions?</b><br/>
Would like the simple answer or the long answer first? That's what I thought, short answer first. Basically, sessions are a storage method - there's no real mystery about them other than the special way that PHP handles them. You can read and write to them just like you can any other PHP variable, with one very handy difference. Sessions are special - they get to zip around from page to page, following the user.
</p>
<p>
Confused? I hope not, it's a pretty simple thing to get. Sessions are something built into PHP to allow data to be shared between pages - like login information or the path a user's taking through the site. By default, when a session is started up, a file is written to the temporary directory on the web server to contain the information for you. It's all automatic - you don't have to mess with any of the file functionality in PHP to work with it.
</p>
<p>
PHP tracks which file it's using for which user based on the session ID. This is a string of letters and numbers that are (ideally) unique to the visitor, making it easy for PHP to match the viewer and the file.
</p>
<p>
<b>Why do I want to use them?</b><br/>
So, now you know what they are, but do you see the advantage to them? Why can't I just pass the data around on the URL string? Well, doing something like that is a choice (and is often combined with sessions), but gets to be a little bit of a problem when working with larger values. Sessions are also a bit more secure than just passing things around in a GET request (on the URL line). You wouldn't want to pass user information all around your site on the end of a URL, would you? Not only is that a security issue waiting to happen, but then it's also saved in the person's browser history. Bad news all around.
</p>
<p>
<b>How do I use them?</b><br/>
And now we get to the big question (and hopefully some code, eh?) - how to use these uber-handy, simple, slick little bits of functionality PHP has to offer. Well, thankfully, the answer's pretty simple - and it all starts with one function:
</p>
[php]
<?php
session_start();
?>
[/php]
<p>
Pretty simple, right? There has to be more to it, right? Wrong. Really, that's all it takes to start using sessions in your code. Keep in mind, though, that to be able to use your session on other pages, a call to session_start must be the first thing on every page. So, you have your session started - what are you going to do with it? Well, as I mentioned before, sessions are really just a handy place to store things. For example, if I started up my session and wanted to put a value of "enygma" into the "username" place, I'd do something like:
</p>
[php]
<?php
session_start();
$_SESSION['username']="enygma";
?>
[/php]
<p>
You'll notice two new things here - first is the format: $_SESSION[]. $_SESSION is one of the special variables that PHP handles internally, known as the superglobals. These variables (such as $_SESSION, $_GET, and $_POST) all have their places in PHP's functionality. They're also all arrays to make storing lots of data in them (or pulling from them) a much simpler process. So, using a simplified syntax to enter the value into the array, we push the value "enygma" into the $_SESSION array under the key of 'username'.
</p>
<p>
Get it? Got it? Good! On with the show!
</p>
<p>
<b>Okay, so can you give me an example?</b><br/>
Now we get down to the fun part, applying this knowledge to a simple, small example - just passing data from one page to another. There's two files we'll create: file1.php and file2.php. File1.php will be the one setting the data and file2.php will just display what we set before. Now here's some code...
</p>
File1.php:<br/>
[php]
<?php
session_start();
$_SESSION['username']="enygma";
echo '<a href="file2.php">go to file2</a>';
?>
[/php]
<br/>
File2.php:<br/>
[php]
<?php
session_start();
echo $_SESSION['username'];
?>
[/php]
<p>
It's a very simple example, but it gets the main idea across - bridging the gap between pages with the help of sessions. There's lots more to learn about sessions and some other handy ways to use them, but that'll have to be for another time. For now, play with the examples provided here, and enjoy the new-found freedom of being able to connect pages together without all of that messy URL line handling.
</p>]]></description>
      <pubDate>Tue, 27 Jun 2006 13:43:37 -0500</pubDate>
    </item>
  </channel>
</rss>
