News Feed
Jobs Feed
Sections




News Archive
feed this:

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

PHPMaster.com:
Lesser-Known "Features" of PHP's OO Model
July 22, 2013 @ 12:21:22

On PHPMaster.com there's a new tutorial from Lorna Mitchell about some of the lesser known OOP features that are built in to the PHP language. She talks about things like interface inheritance, private properties and autoloading and type hints.

The vast majority of today's applications written in PHP are object-oriented, and in general the core OOP concepts are pretty well understood by PHP developers. This article pushes the boundary of your understanding and shows you some tricks, or potential pitfalls depending on your perspective, of OOP in PHP.

Besides the ones mentioned above, she also looks at abstract classes and their use as well as the use of "finally" to handle the cleanup after exceptions.

0 comments voice your opinion now!
features oop model language private inheritance typehint finally

Link: http://phpmaster.com/lesser-known-features-of-phps-oo-model

PHPEasy.co.uk:
Tutorial Type Hinting in PHP
August 01, 2012 @ 12:14:35

On the PHPEasy.co.uk site there's a quick new tutorial posted about type hinting in PHP - how to use it and why you should use it in your code.

Type hinting allows you to tell a function which data type to accept for its arguments. When you call a function, if a type hint is present, the argument passed will be checked to make sure it is of the type specified. If a function has control over what it accepts as an argument, then it is more likely that the code within the function will have the data it needs to perform the task it is intended for.

He includes two code examples - one not using a type hinting on the parameters (assuming the correct arguments) and another forcing the input of the "PrintGuitar" method to be a "Guitar" object instance. You can find out more about type hinting in the PHP manual.

0 comments voice your opinion now!
tutorial typehint example code beginner


James Fuller's Blog:
Enforcing contracts in your PHP functions and methods
March 23, 2012 @ 08:37:11

James Fuller has a new post to his blog today about a way you can enforce contracts in your PHP using a combination of type hinting and value checking.

Design by contract is an important concept for controlling what type of input your methods or functions can receive. One of the most dangerous features of PHP is that functions will still execute even when they are missing required arguments, by emitting a warning instead of an error. In this post, I am going to walk through some of the solutions available to deal with this problem.

He shows how to alter a basic function to first use type hinting to catch when a variable is the wrong type (in this case checking for an array and stdClass) which causes a Fatal error and makes the function not execute. He includes sample code for the type/value checking option and also includes a suggestion of using PHPUnit's assertions as another option.

Finally, he introduces the ContractLib tool (from Stuart Herbert) that makes use of closures to enforce checks - his example checks to see if something is a string and that it's not empty.

0 comments voice your opinion now!
enforce contract function exception typehint contractlib


DZone.com:
Reuse your closures with functors
December 29, 2010 @ 10:50:19

On DZone.com there's a new tutorial from Giorgio Sironi about reusing closures with the help of functors (a special kind of object instancing done in PHP with the help of __invoke).

I like PHP closures and their superset, anomyous functions, as they implement the Command pattern very well when used correctly. However I feel that sometimes they are: difficult to reuse and difficult to force contracts on. [...] What if we wanted instead, a closure which we can instance even more than one time (maybe with different variables), and that we could type hint?

He shows how to make this possible with a functor created using the __invoke magic method of PHP to handle the request to an object like a function. He includes some sample code to show it in action - a basic callback (SquareCallback) and how it compares to calling a normal closure. It also shows something a bit more technical, an "AdderCallback" class that can be defined as a type hint on a function.

0 comments voice your opinion now!
closure functor invoke snippet example callback typehint


OpenSky Blog:
Inheritance in PHP, or why you want to use an Interface or an Abstract class...
November 30, 2010 @ 11:09:17

On the OpenSky blog today there's a new post about object oriented development and class structure. More specifically about how you should use interfaces and abstract classes if you want to truly lock down the methods of your classes.

Every class has an interface, every class can be typehinted as a method argument, typehint lets you specify the collaborator requirements. Not every typehint is the same.

He includes an example of how, despite a "BankAccount" type hint, classes for different types of accounts can be created and methods from the parent can be overwritten. To help remedy the situation, he shows a more structured example that uses interfaces/an abstract class to define the BankAccount structure, setting requirements on the child class functionality.

0 comments voice your opinion now!
typehint interface abstract class example structure


Ilia Alshaetsky's Blog:
Type Hinting - Conclusion
July 23, 2009 @ 08:27:52

Following up on all of the craziness that was caused by the suggestion of including type hinting into the PHP core, Ilia Alshaetsky recently posted some of his thoughts on how things turned out and where he'll go from here.

Unfortunately, while initial outpouring of support for inclusion of type hinting into PHP 5.3 and 6 was substantial, it all kinda petered out once more people started voting. To be specific, there is a large amount of support for the feature in general, but very few people seem to think it should go into 5.3. [...] That said, at work we will transition to 5.3 soon, probably around 5.3.2/3 time, so I do need a type hinting patch. To that effect I've cleaned up and improved the old patch based on comments from various people, notably Stas and Dmitry (thank guys) and made a copy that I intend to use for work.

He talks about how the patch works - no more virtual types and no casting support. Three things have been changed to make it all work: modification to the parser, added a getTypeHint function, properly parse type hints in the code.

0 comments voice your opinion now!
release patch typehint


Tibo Beijen's Blog:
Explicit PHP6?
June 12, 2009 @ 10:27:26

Tibo Beijen takes his own look at some of what PHP6 will have to offer and talks a bit about how it would have helped him in a current project:

I'm currently working on [a project] where I've been experimenting with 'domain objects' having 'scalar' or 'value' objects as properties (more on that later).

The talks specifically about two of the upcoming features - type hinting and the __cast magic method that allows you to correctly cast a variable into a different type. He illustrates with some code that handles and manipulates user data and handles exceptions on an incorrect type cast.

0 comments voice your opinion now!
typehint cast php6


Max Horvath's Blog:
Type Hints for scalar values - PHPTypeSafe now compatible with current PHP 5.3
May 26, 2009 @ 11:15:52

Max Horvath has released a new version of his PHPTypeSafe (a tool to give you type safety for method calls). Now it's compatible with the upcoming PHP 5.3 release too.

It is now compatible with the current version of PHP 5.3 (right now it is RC2). I think we won't see any major changes with the namespace handling in PHP 5.3 anymore, so I felt I could upgrade the library.

You can clone the source for the tool from its github page and check out the blog post for an example of it in use.

0 comments voice your opinion now!
typehint library phptypesafe


Stubbles Blog:
Two New PHP6 Wishlist Items
February 21, 2007 @ 10:29:00

Launching off from this previous post on the Stubbles Blog, two more posts (from two other developers) have been posted on the same subject:

  • In this new post, Frank Kleine takes things a step further and talks about the same kind of type hinting but for more types - including arrays and objects. Examples of how he'd expect things to work followed.
  • The other post is from Stephan Schmidt again and varies slightly off into the world of Namespaces to be included in PHP6 along with the use of annotations.
Be sure to check out the comments on each for some more interesting tidbits...

0 comments voice your opinion now!
php6 wishlist namespace annotation typehint array object php6 wishlist namespace annotation typehint array object


Stubbles Blog:
My wishlist for PHP 6, pt1 The 'object' type hint
February 20, 2007 @ 09:07:00

Stephan Schmidt has posted some thoughts about type hinting in PHP5 (what's there) and the same kind of functionality for objects in PHP6 (what's to come).

I was very pleased, that PHP 5 introduced type hints, although they are not available for primitives like string, int, boolean, etc. Still, I'd like to see the object type hint introduced in any future version of PHP that allows me to specify, that a method or function only accepts an object, regardless of the type of the object.

He gives the example of Java's handling of the functionality and what PHP does with the same type of code. He also mentions that, via the Reflection API extension, some of this is possible already, so why not just include it from here on out?

0 comments voice your opinion now!
typehint php5 php6 object java common base class typehint php5 php6 object java common base class



Community Events











Don't see your event here?
Let us know!


podcast example application release opinion unittest install overview framework component hack language security code package symfony2 facebook composer hhvm introduction

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework