Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Community News:
PHP-Dev Mailing List - PHP 5.4 alpha
Nov 04, 2010 @ 14:03:59

As is mentioned in this message to the php-dev mailing list from Derick Rethans, there's a desire to start on the next alpha release version for PHP - the 5.4.x series.

We're a bit further along now; and with the typehinting resolved (http://thread.gmane.org/gmane.comp.php.devel/62298/focus=62858) I want to start started with PHP 5.4 with the first alpha on Wednesday, November 24th. There are a few things that need sorting out and/or clarification: Annotations, Lemon rewrite, APC in trunk.

Keep in mind that this is only the very start of the process, so depending on any bumps along the way, PHP 5.4 may still be a bit out. Check the main PHP site for updates on betas and final releases as they become available.

tagged: alpha version phpdev mailinglist

Link:

Community News:
Namespaces in PHP6
Jul 06, 2007 @ 16:10:00

Spurred on by a recent post to the php-dev mailing list by Dmitry Stogov the ever-popular "namespaces discussion" has surged back to the top of everyone's list. Dmitry proposed a patch for PHP6 that would easily implement namespaces for the upcoming version.

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.

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 included code in his mailing list post to illustrate how it would all work.

Several others in the PHP community have jumped on and are already talking about this new proposal:

tagged: namespace php6 patch declaration mailinglist phpdev namespace php6 patch declaration mailinglist phpdev

Link:

Community News:
Namespaces in PHP6
Jul 06, 2007 @ 16:10:00

Spurred on by a recent post to the php-dev mailing list by Dmitry Stogov the ever-popular "namespaces discussion" has surged back to the top of everyone's list. Dmitry proposed a patch for PHP6 that would easily implement namespaces for the upcoming version.

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.

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 included code in his mailing list post to illustrate how it would all work.

Several others in the PHP community have jumped on and are already talking about this new proposal:

tagged: namespace php6 patch declaration mailinglist phpdev namespace php6 patch declaration mailinglist phpdev

Link:

APress' Inside Open Source Blog:
Namespaces in PHP 6
Nov 17, 2006 @ 13:30:53

As this post over on APress' Inside Open Source blog today, there's a discussion that's been going on over on the php-dev mailing list about namespaces in PHP6.

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.

That's Jason Gilmore talking there, and he goes on to reinforce something Rasmus Lerdorf has said about namespaces in the past - they'll implement them when they find the right way (in the PHP spirit) to do it.

tagged: namespace mailing list phpdev php6 enable default namespace mailing list phpdev php6 enable default

Link:

APress' Inside Open Source Blog:
Namespaces in PHP 6
Nov 17, 2006 @ 13:30:53

As this post over on APress' Inside Open Source blog today, there's a discussion that's been going on over on the php-dev mailing list about namespaces in PHP6.

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.

That's Jason Gilmore talking there, and he goes on to reinforce something Rasmus Lerdorf has said about namespaces in the past - they'll implement them when they find the right way (in the PHP spirit) to do it.

tagged: namespace mailing list phpdev php6 enable default namespace mailing list phpdev php6 enable default

Link:

Tutorial:
An Introduction to OOP in PHP
Jun 28, 2006 @ 17:14:05

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.

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.

Read on for the rest of the tutorial!

What's so cool about OOP anyway?
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.

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.

The Object of Our Affections
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:

[php] [/php]

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.

Ah! Now to the fun part! Object creation! (Well, not so much fun as the next step in the process).

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:

[php] [/php]

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.

Putting Properties Into the Mix
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:

[php] var $my_var = 'testing'; function myClass(){ }

} $mine=new myClass(); echo $mine->my_var; ?> [/php]

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:

[php] echoMe(); ?> [/php]

Running this script will result in the text "me" being output to the page. Pretty simple, huh?

Taking the Next Step
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:

[php] 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]

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.

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:

[php] setHeight(30); $box->setWidth(300); $box->displayBox(); ?> [/php]

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).

Wrapping it all up
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 their great resource on the topic. It has the answers to the questions you'll want to know now that you're finished with this little tutorial.

tagged: phpdev tutorial introduction object-oriented programming beginner phpdev tutorial introduction object-oriented programming beginner

Link:

Tutorial:
An Introduction to OOP in PHP
Jun 28, 2006 @ 17:14:05

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.

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.

Read on for the rest of the tutorial!

What's so cool about OOP anyway?
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.

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.

The Object of Our Affections
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:

[php] [/php]

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.

Ah! Now to the fun part! Object creation! (Well, not so much fun as the next step in the process).

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:

[php] [/php]

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.

Putting Properties Into the Mix
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:

[php] var $my_var = 'testing'; function myClass(){ }

} $mine=new myClass(); echo $mine->my_var; ?> [/php]

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:

[php] echoMe(); ?> [/php]

Running this script will result in the text "me" being output to the page. Pretty simple, huh?

Taking the Next Step
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:

[php] 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]

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.

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:

[php] setHeight(30); $box->setWidth(300); $box->displayBox(); ?> [/php]

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).

Wrapping it all up
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 their great resource on the topic. It has the answers to the questions you'll want to know now that you're finished with this little tutorial.

tagged: phpdev tutorial introduction object-oriented programming beginner phpdev tutorial introduction object-oriented programming beginner

Link:

Tutorial:
A Simple Sessions Tutorial
Jun 27, 2006 @ 18:43:37

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.

Click here for the full article!

What are sessions?
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.

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.

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.

Why do I want to use them?
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.

How do I use them?
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:

[php] [/php]

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:

[php] [/php]

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'.

Get it? Got it? Good! On with the show!

Okay, so can you give me an example?
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...

File1.php:
[php] go to file2'; ?> [/php]
File2.php:
[php] [/php]

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.

tagged: phpdev tutorial simple session introduction beginner phpdev tutorial simple session introduction beginner

Link:

Tutorial:
A Simple Sessions Tutorial
Jun 27, 2006 @ 18:43:37

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.

Click here for the full article!

What are sessions?
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.

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.

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.

Why do I want to use them?
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.

How do I use them?
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:

[php] [/php]

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:

[php] [/php]

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'.

Get it? Got it? Good! On with the show!

Okay, so can you give me an example?
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...

File1.php:
[php] go to file2'; ?> [/php]
File2.php:
[php] [/php]

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.

tagged: phpdev tutorial simple session introduction beginner phpdev tutorial simple session introduction beginner

Link:


Trending Topics: