News 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:
Type Hinting in PHP
March 05, 2012 @ 13:19:24

On PHPMaster.com today there's a new tutorial posted about using type hinting in your PHP applications to restrict the values passed into your methods.

Since PHP 5 you can use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, PHP will check whether or not the arguments are of the specified type. If not, the run-time will raise an error and execution will be halted.

Included in the post are code examples showing how to define custom types in a function definition and what happens if you pass the wrong type in. Also mentioned is one of the main limitations to hinting - the fact that it can't be used on default PHP variable types.

0 comments voice your opinion now!
type hint example method default


Andrew Eddie's Blog:
Making the most out of Code Assist in Eclipse/PDT and Zend Studio for PHP
December 01, 2011 @ 12:55:32

Andrew Eddie has posted a helpful tutorial for Eclipse users out there showing how to get the most our of code assist in Eclipse PDT/Zend Studio.

One of the powerful features of an IDE like Eclipse is the ability for it to "read" your code and give you some assistance about your API as you type. This could include things like class property or methods names, constants, functions, argument lists, and so on. Eclipse/PDT and ZendStudio do this by parsing a PHP class directly, but they also look at your docblocks and some other special comments where the raw PHP is not enough. This article is a bag of tricks that help you get the most out of code assistance using Eclipse/PDT or Zend Studio in those awkward corners of your code that you might have through previously inaccessible.

Among his tips are things like adding "@var" declarations to help with code completion, type hinting on methods/functions, using the "@property" annotation and using a "this" trick to override what class the IDE sees as the local object. ,/p>

0 comments voice your opinion now!
eclipse zendstudio ide hint trick docblock annotation codeassist


Zoomzum Blog:
10 Powerful PHP Regular Expression For Developers
July 27, 2011 @ 09:02:10

On the Zoomzum blog there's a new post with ten regular expressions PHP developers can use to accomplish some common tasks (like email validation and date formatting checks).

Regular expression for the PHP developers, on of the most popular tool for validating data is the regular expression. In this list we provides some validation - string match, password match validation, email address validation, date format and many more which helps developer to make their application more fast and easy to execute. [...] Have you note that, regular expressions are more slower than the basic string function, its takes a short time to execute than any others.

Included in their list are things like:

  • Password Match Validation
  • Validate URL
  • Validate URL using Preg_match
  • UK Postcode Validation
  • SSN,ISBN and Zipcode Validation

A few of these could be done with either one or two string calls or some of the filtering functions that are included in PHP.

5 comments voice your opinion now!
regular expression hint list validate regexp


Chris Shiflett's Blog:
Sorting Multi-Dimensional Arrays in PHP
July 01, 2011 @ 09:48:26

In a sort of "remind himself later" kind of post, Chris Shiflett has shared a quick example of a common task PHP developers face sorting a multi-dimensional array.

Every time I need to sort a multi-dimensional array in PHP, I have to remind myself how to do it. It's not quite as quick and easy to look up as most things, so I'm going to blog a quick example. I've always felt like there must be a better way to do this, so please let me know if there is, and I'll update this post accordingly.

His method sorts a multi-dimensional array of user data using the array_multisort function together with a list of the usernames in each record. In the comments, people recommend using usort instead with a callback+closure combo that keeps things nice and clean.

0 comments voice your opinion now!
sort multidimensional array hint


Sebastian Bergmann's Blog:
Scalar Type Hints in PHP 5.3.99
November 24, 2010 @ 10:04:06

In the theme of other recent posts mentioning the scalar type hinting that has been included in the main line of code that is headed towards the next PHP release, Sebastian Bergmann has a new post about their inclusion in PHP 5.3.99 (yes, that's PHP 5.4) and the new syntax it introduces.

In a nutshell, this means that PHP 5.3.99 introduces new syntax -- scalar type hints -- but no new semantics. The latter can either be implemented as an extension written in C/C++, in userland PHP code, or in a tool that statically analyzes the code.

He includes an example fro userland with a "php_check_parameters" function that looks at the arguments of the current method and uses Reflection to check against the type hints for the correct value type.

0 comments voice your opinion now!
reflection scalar type hint feature userland


Johannes Schluter's Blog:
Scalar type hints in PHP trunk
August 09, 2010 @ 08:44:23

As Johannes Schluter mentions in his latest blog post, another new feature has been added to the trunk of the PHP project - scalar type hints.

So in my blog series I try to cover all additions to PHP trunk so I have to mention scalar type hints.

He gives examples of both simple hinting (ensuring a value is a float) and a more complex example setting an attribute on a PDO connection (checking for an integer). Sample error messages are included as well. He also gives some advice on mixing strong and weak typing in your apps - it's like "opening a can of worms".

0 comments voice your opinion now!
scalar type hint trunk feature language


Ilia Alshanetsky's Blog:
Scalar Type Hints are Here!
May 24, 2010 @ 11:48:19

Ilia Alshanetsky has a quick new post with some great news for some PHP developers out there - scalar type hinting has been introduced into the latest trunk of the PHP project.

About an hour ago, something I've been fighting for almost 2 years happened. The Scalar Type Hinting patch for PHP (the one I wrote almost a year ago) has been adjusted for PHP's trunk tree and committed by Derick.

You can see some of the type hinting features already included in PHP in the manual. This patch adds hinting ability for the other (non-object and non-array) variable types.

1 comment voice your opinion now!
scalar type hint trunk patch


Alex Netkachov's Blog:
PHP micro-optimization tips
March 10, 2009 @ 08:47:38

For those trying to squeeze the absolute most out of their applications, check out these suggestions from Alex Netkachov on a few "micro" kinds of things that could help speed things up in the long run.

Why "micro-"? Because changing logic of your application may give you much better performance boost then applying all these tips. But they still can make your code better. You always need to output something, why do not use "echo" instead of "print"?

He has a list of about thirty things you can do including:

  • calling a function is faster then calling a static method
  • accessing an initialized variable is faster then accessing an uninitialized variable
  • avoid @ (error control operator)
  • adding method parameter type hint increases calling time
  • cache page output or result of resource-consuming functions
  • pre-increment (++$i) is faster then post-increment ($i++)
  • an array is a faster alternative to a class with several fields

These hints aren't going to make amazing differences in your code, but they could help get you out of a sticky place where your code just doesn't want to behave.

0 comments voice your opinion now!
microoptimization tip small hint


Misko Hevery's Blog:
Guide Writing Testable Code
January 07, 2009 @ 10:29:39

In this slightly older (Nov 2008) but useful post to Misko's blog, he takes a look at a few common flaws that you should avoid in writing up your code (in any language really).

To keep our code at Google in the best possible shape we provided our software engineers with these constant reminders. Now, we are happy to share them with the world.

Here's the list of the flaws:

  • Flaw #1: Constructor does Real Work
  • Flaw #2: Digging into Collaborators
  • Flaw #3: Brittle Global State & Singletons
  • Flaw #4: Class Does Too Much

Each includes some warning signs to keep an eye out for to see if you and your code might be straying the wrong way.

0 comments voice your opinion now!
testable guide hint flaw avoid warning sign


Jani Hartikainen's Blog:
Static/Dynamic typing sweet spot
October 08, 2008 @ 10:24:28

In a new post today Jani Hartikainen takes a look at one of the things that makes PHP both powerful and unpredictable at times - its variable typing, static versus dynamic.

It seems that PHP has moved a bit towards static typing, and languages like C# seem to be implementing some dynamic features. Are we going towards a "mixed" language with static and dynamic typing? What's the "sweet spot" between completely static (like C++) and completely dynamic typing (like Python)?

He compares the benefits of static and dynamic, and a particular place where PHP falls a bit flat - type hinting in certain situations. He agrees, though, that dynamic typing is "the way to go" since it gives the developer the flexibility they might need for more "typing tricks" in their apps. Oh, and "duck typing"...

0 comments voice your opinion now!
static dynamic typing type variable hint compare



Community Events





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


configure bugfix interview wordpress framework laravel code list podcast series threedevsandamaybe release api introduction developer language community application library project

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