PHP Posts

Weekly PHP Test What does the following code echo out: [php] if('ORANGE' > 'apple') { echo 'orange'; } else { echo 'apple'; } [/php] ... ... ... ... ... ... ANSWER: [crayon]apple[/crayon] The...

Read more

PHP 5.4 – First Glances PHP 5.4 is just around the corner, and with it several new features and enhancements.  Here are just a few of the features I've had the chance to play with that may be useful. Arrays Finally, in PHP...

Read more

Weekly PHP Test True or False: In PHP 5.4 Traits can have properties? .. .. .. .. .. .. .. .. .. .. ANSWER: True! In PHP 5.4 Traits may contain properties as long as properties...

Read more

smrtClass smrtClass is a restrictive data type class based on PHP's stdClass. Instead of allowing the free creation and setting of properties as the stdClass does, smrtClass requires properties to be setup first...

Read more

Weekly PHP Test What does the following code produce in PHP 5.0+ [php] $var = (object) array('0'=>'hello world'); if(isset($var[0])) { die('Is Set'); } die('Not Set'); [/php] . . . . . . ANSWER: Fatal...

Read more

  • Prev
  • Next

Cool Tricks

Category : Just for Fun

More cool tricks

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

Weekly PHP Test

Category : Weekly Test

What does the following code echo out:

PHP
1
2
3
4
5
if('ORANGE' > 'apple') {
echo 'orange';
} else {
echo 'apple';
}

→ Continue

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

PHP 5.4 – First Glances

Category : Articles, PHP, Tutorials

PHP 5.4 is just around the corner, and with it several new features and enhancements.  Here are just a few of the features I’ve had the chance to play with that may be useful.

Arrays

Finally, in PHP you can now use the short bracket syntax for arrays.  While really not a huge deal, this aligns PHP with many of the other programming languages, and will probably make switching from another language just a little easier (although array(1,2,3) is pretty simple).

PHP
1
2
3
4
5
$oldArray = array(1,2,3);
echo (string) $oldArray[1]; // 2
$newArray = [1,2,3];
echo (string) $newArray[1]; // 2

Another nice feature in regards to arrays is Array Dereferencing, or being able to access an array object directly off of a method or function.
PHP
1
2
3
4
5
6
7
8
9
10
11
function getArray()
{
return array(1,2,3);
// or return [1,2,3]; in PHP 5.4 ;)
}
// PHP 5.3
echo (string) getArray()[1]; // error
// PHP 5.4
echo (string) getArray()[1]; // 2

Class Instantiation Chaining

Also in PHP 5.4, class instantiation chaining was added, allowing you to chain off of the instantiation of a class by using paranthesis.

PHP
1
2
3
4
5
6
7
8
9
10
11
class Test
{
public function hello()
{
echo 'hello world';
}
}
// in PHP 5.3 this would be an error
// in PHP 5.4 it echos 'hello world'
echo (new Test())->hello();

Traits

Ok, this is a super simple overview of traits, but the addition of traits provides the flexibility of “extending” multiple classes without having to go through an entire chain.  Unlike many languages, PHP Traits can also contain properties, however, if the class contains a property with the same name it will throw an E_STRICT warning IF the class property === the trait property, or a fatal error if the class property !== the trait property.

To use a trait, you would write something like this:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
trait MyTrait()
{
public $MyProperty = 'hello';
public function MyMethod()
{
echo 'world';
}
}
class MyClass()
{
use MyTrait;
public function DoStuff()
{
echo $this->MyProperty;
echo ' ';
$this->MyMethod();
}
}
(new MyClass())->DoStuff(); // echos out 'hello world'

More to Come!

There’s a lot more features in PHP 5.4 that I still need to explore, including PHP Session Uploading, improved Closures, and a built in HTTP server.  So stay tuned and I will try to give a quick overview of those as well as soon as I can get enough time to play with them :)

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

Weekly PHP Test

Category : Weekly Test

True or False: In PHP 5.4 Traits can have properties?

→ Continue

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

smrtClass

Category : smrtClass

smrtClass is a restrictive data type class based on PHP’s stdClass. Instead of allowing the free creation and setting of properties as the stdClass does, smrtClass requires properties to be setup first with a specific type or regex requirement. Data is then checked against this requirement when the property is modified, and if inconsistent with the data requirements throws an exception.

smrtClass is designed to provide an added layer of security to PHP applications, and while it does not provide 100% protection, it does signigicantly help to prevent malicious, dangerous, or malformed data from making its way into the applications crucial systems.

smrtClass also contains locking mechanisms to prevent data from being deliberately or accidentally modified.

CLASS METHODS

 
== smrtClass::add ==
smrtClass::add( $var_name, $var_type = ['string'|'int'|'bool'|'object'|'array',|'date'|'datetime'|'timestamp'|'email'|'url'], $value = null );

PHP
1
2
3
4
5
6
7
8
9
10
$test = new smrtClass();
$test->add('number', 'int');
$test->number = '10'; // converted to integer
echo gettype($test->number): // displays "integer"
$test->add('string');
$test->string = 'Hello World'; // set as a string
$test->add('emailAddress', 'email');
$test->emailAddress = 'test@test.com'; // validated to be an email and set as string

== smrtClass::addRegex ==
smrtClass::addRegex( $var_name, $regex, $value = null );

PHP
1
2
3
4
5
6
7
8
$test = new smrtClass();
$test->addRegex('numberOnly', '/^[0-9]+$/');
$test->numberOnly = 10; // valid
$test->numberOnly = 'ten'; // throws exception
$test->add('alphaOnly', '/^[a-z]$/i');
$test->alphaOnly = 'Hello'; // valid
$test->alphaOnly = 'Hello world'; // throws an exception because of the space

== smrtClass::set ==
smrtClass:set( $var_name, $value )

PHP
1
2
3
4
$test = new smrtClass();
$test->add('name');
$test->set('name', 'Michael');
$test->name = 'Michael'; // does the same thing as set

== smrtClass::remove ==
smrtClass:remove( $var_name )

PHP
1
2
3
4
5
$test = new smrtClass();
$test->add('name');
$test->name = 'Michael';
$test->remove('name'); // removes the name property
unset($test->name); // does the same thing as remove

== smrtClass::listTypes ==
smrtClass::listTypes()

returns an array of supported types such as string, int, bool, etc.

PHP
1
2
$test = new smrtClass();
$test->listTypes();


Installing smrtClass

smrtClass can be run on any server utilizing PHP 5.3

Coding Restrictions

  1. smrtClass is limited to the same property naming restrictions as stdClass
  2. smrtClass currently does not allow for data types to be changed

Bugs

  • No known errors


Awards (Yay)
- listed in the order received
We’re working on this… lol

Usage
smrtClass was released under the GPL version 2 license.

Download

http://www.phpclasses.org/smrtClass

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

Weekly PHP Test

Category : Weekly Test

What does the following code produce in PHP 5.0+

PHP
1
2
3
4
5
6
$var = (object) array('0'=>'hello world');
if(isset($var[0])) {
die('Is Set');
}
die('Not Set');

→ Continue

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

A Tribute to Steve Jobs

Category : Uncategorized

(February 24, 1955 – September 5, 2011)



Thank you for all you have done for technology over the years, for bringing us the Apple, the Macintosh Color Classic, the iMac, the iPod, the iPhone, and recently the iPad… your amazing innovative will be greatly missed. Rest in Peace.

Steve Jobs in Pictures

Job’s Incredible Legacy

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

Real Angry Birds

Category : Just for Fun

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

Weekly PHP Test

Category : Weekly Test

In PHP empty(array()) returns true, where-as empty(array(1)) returns false. What does the following code produce in PHP 5.0+

PHP
1
2
3
4
5
6
$var = (object) array();
if(empty($var)) {
die('Is Empty');
} else {
die('Is Not Empty');
}

→ Continue

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

Weekly PHP Test

Category : Weekly Test

Assuming $cache->load($key) contains a cache of “yes” and $mediaCacheEnabled = true, what will the var_dump of $data return in this case?

PHP
1
2
3
4
5
if (!$data = $cache->load($key) || !$mediaCacheEnabled) {
$data = 'no';
}
var_dump($data);

→ Continue

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share