What’s new in PHP 5.3?
Namespaces
Before the days of object oriented PHP, many application developers made use of verbose function names in order to avoid namespace clashes. Wordpress, for example, implements functions such as wp_update_post and wp_create_user. The wp_ prefix denotes that the function pertains to the Wordpress application, and reduces the chance of it clashing with any existing functions.
In an object oriented world, namespace clashes are less likely. Consider the following example code snippet, which is based on a fictional blogging application:
<?php
class User {
public function set( $attribute, $value ) { ... }
public function save() { ... }
}
$user = new User();
$user->set('fullname', 'Ben Balbo');
$user->save();
In this example, the save method will not clash with any other method, as it is contained within the User class. There’s still a potential issue though: the User class might already be defined by some other part of the system if, for example, the blogging application runs within a content management system.
The solution to this issue is to use the new namespaces keyword. Taking the above code again, consider the following sample files:
<?php
namespace MyCompany::Blog;
class User {
public function set( $attribute, $value ) {
$this->$attribute = $value;
}
public function save() {
echo '<p>Blog user ' . $this->fullname . ' saved</p>';
}
}
<?php
$user = new MyCompany::Blog::User();
$user->set('fullname', 'Ben Balbo');
$user->save();
On the surface, the advantages offered by namespacing our function might not be immediately obvious — after all, we’ve simply changed MyCompany_Blog_User to MyCompany::Blog::User. However, we can now create a User class for the CMS using a different namespace:
<?php
namespace MyCompany::CMS;
class User {
public function set( $attribute, $value ) {
$this->$attribute = $value;
}
public function save() {
echo '<p>CMS user ' . $this->fullname . ' saved</p>';
}
}
We can now use the classes MyCompany::Blog::User and MyCompany::CMS::User.
The use Keyword
Addressing classes using the full namespace still results in lengthy calls, and if you’re using lots of classes from the MyCompany::Blog namespace, you might not want to retype the whole path to the class every time. This is where the use keyword comes in handy. Your application will most likely use a number of different classes at any given time. Say, for example, the user creates a new post:
<?php
use MyCompany::Blog;
$user = new Blog::User();
$post = new Blog::Post();
$post->setUser( $user );
$post->setTitle( $title );
$post->setBody( $body );
$post->save();
The use keyword is not restricted to defining namespaces in which to work. You can also use it to import single classes to your file, like so:
<?php
use MyCompany::Blog::User;
$user = new User();
Namespace Aliases
Earlier, I pointed out that one advantage of namespacing is the ability to define more than one class with the same name in different namespaces. There will obviously be instances where those two classes are utilized by the same script. We could just import the namespaces, however, we also have the option of importing just the classes. To do so, we can use namespace aliasing to identify each class, like so:
<?php
use MyCompany::Blog::User as BlogUser;
use MyCompany::CMS::User as CMSUser;
$bloguser = new BlogUser();
$bloguser->set('fullname', 'John Doe');
$bloguser->save();
$cmsuser = new CMSUser();
$cmsuser->set('fullname', 'John Doe');
$cmsuser->save();
Class Constants
Constants are now able to be defined at the class level! Note that class constants are available when you’re importing namespaces, but you cannot import the constant itself. Here’s an example of how we might use them:
<?php
namespace MyCompany;
class Blog {
const VERSION = '1.0.0';
}
<?php
echo '<p>Blog bersion ' . MyCompany::Blog::VERSION . '</p>';
use MyCompany::Blog;
echo '<p>Blog version ' . Blog::VERSION . '</p>';
use MyCompany::Blog::VERSION as Foo;
echo '<p>Blog version ' . Foo . '</p>';
This will result in the following output:
Blog bersion 1.0.0
Blog version 1.0.0
Blog version Foo
Namespaced Functions
The use of static class methods has deprecated the use of functions in the object oriented world in which we now live. However, if you do need to add a function to your package, it too will be subject to namespacing!
Here’s an example:
<?php
namespace bundle;
function foo() { echo '<p>This is the bundled foo</p>'; }
foo(); // This prints 'This is the bundled foo'
<?php
function foo() { echo '<p>This is the global foo</p>'; }
require( 'lib/bundle.class.php');
bundle::foo(); // This prints 'This is the bundled foo'
foo(); // This prints 'This is the global foo'
The Global Namespace
The global namespace is an important consideration when you’re dealing with functions. In the previous example, you’ll notice that there’s no direct way of calling the global foo function from within the bundle code.
The default method of resolving calls to functions is to use the current namespace. If the function cannot be found, it will look for an internal function by that name. It will not look in other namespaces automatically.
To call the global foo function from within the bundle namespace, we need to target the global namespace directly. We do this by using a double colon:
<?php
namespace bundle;
function foo() { echo '<p>This is the bundled foo</p>'; }
foo(); // This prints 'This is the bundled foo'
::foo(); // This prints 'This is the global foo'
Css for rounded corner
#inner { width:500px; border:3px solid #518aa4; background:#FFFFFF; padding: 2px; font: 12px Verdana, Arial, Helvetica, sans-serif; -moz-border-radius: 15px 0 15px 0; }
How could i set in php allow_call_time_pass_reference by .htaccess code
set php allow_call_time_pass_reference in .htaccess
I just spent about an hour trying to figure this out, but when you have a php application that requires call time pass reference to be enabled, you can set in the .htaccess file.
the trick is that you have to set it to “1″ and not “on” as is indicated on the PHP support forums. here’s the code:
php_flag allow_call_time_pass_reference 1
Javascript email validaion
var emailExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var EmailAddress = trim(document.frm.To.value);
if(trim(EmailAddress)==”") {
document.getElementById(’Error_Div’).innerHTML = “Please enter valid email id”;
document.getElementById(’Error_Div’).style.display=”block”;
document.frm.To.focus();
return false;
}
if(!EmailAddress.match(emailExp)) {
document.getElementById(’Error_Div’).innerHTML = “Please enter valid To email id”;
document.getElementById(’Error_Div’).style.display=”block”;
document.frm.To.focus();
return false;
}
Select at least one radio button
for (var i = 0; i < document.frm.TPACleared.length; i++) {
if (document.frm.TPACleared[i].checked) {
TPACleared_1 = document.frm.TPACleared[i].value;
TPACleared_1 = true;
}
}
if(TPACleared_1 != true){ //if found_it is NOT equal to null, a button HAS been checked
document.getElementById(’alert_div’).innerHTML= ‘Please select a option fot Response For Clearance?’;
document.getElementById(’alert_div’).style.display=’block’;
return false;
}
Javascript Trim Function
// Removes starting whitespaces
function LTrim(value) {
var re = /\s*((\S+\s*)*)/;
return value.replace(re, “$1″);
}
// Removes ending whitespaces
function RTrim(value) {
var re = /((\s*\S+)*)\s*/;
return value.replace(re, “$1″);
}
// Removes leading and ending whitespaces
function trim(value) {
return LTrim(RTrim(value));
}
JavaScript Date Validation
<!—Date Validation——————————————————————–>
/*** DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/) */
// Declaring valid date character, minimum year and maximum year
var dtCh= “-”;
var minYear=1900;
var maxYear=2100;
function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < “0″) || (c > “9″))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag){
var i;
var returnString = “”;
// Search through string’s characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function daysInFebruary (year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)==”0″ && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)==”0″ && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)==”0″ && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
//alert(”The date format should be : mm/dd/yyyy”);
document.getElementById(”alert_date_div”).innerHTML=”The date format should be : mm-dd-yyyy.”;
document.getElementById(”alert_date_div”).style.display=”block”;
//document.getElementById(”date”).focus();
return false
}
if (strMonth.length<1 || month<1 || month>12){
//alert(”Please enter a valid month”)
document.getElementById(”alert_date_div”).innerHTML=”Enter a valid month.”;
document.getElementById(”alert_date_div”).style.display=”block”;
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//alert(”Please enter a valid day”)
document.getElementById(”alert_date_div”).innerHTML=”Enter a valid day.”;
document.getElementById(”alert_date_div”).style.display=”block”;
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//alert(”Please enter a valid 4 digit year between “+minYear+” and “+maxYear)
document.getElementById(”alert_date_div”).innerHTML=”Enter a valid 4 digit year between “+minYear+” and “+maxYear;
document.getElementById(”alert_date_div”).style.display=”block”;
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//alert(”Please enter a valid date”)
document.getElementById(”alert_date_div”).innerHTML=”Enter a valid date.”;
document.getElementById(”alert_date_div”).style.display=”block”;
return false
}
return true
}
function isDate_to_create_profile(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)==”0″ && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)==”0″ && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)==”0″ && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
//alert(”The date format should be : mm/dd/yyyy”);
document.getElementById(”bprofile_baby_name_div”).innerHTML=”The date format should be : mm-dd-yyyy.”;
document.getElementById(”bprofile_baby_name_div”).style.display=”block”;
//document.getElementById(”date”).focus();
return false
}
if (strMonth.length<1 || month<1 || month>12){
//alert(”Please enter a valid month”)
document.getElementById(”bprofile_baby_name_div”).innerHTML=”Please select a valid month.”;
document.getElementById(”bprofile_baby_name_div”).style.display=”block”;
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//alert(”Please enter a valid day”)
document.getElementById(”bprofile_baby_name_div”).innerHTML=”Please select a valid day.”;
document.getElementById(”bprofile_baby_name_div”).style.display=”block”;
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//alert(”Please enter a valid 4 digit year between “+minYear+” and “+maxYear)
document.getElementById(”bprofile_baby_name_div”).innerHTML=”Please select a valid 4 digit year between “+minYear+” and “+maxYear;
document.getElementById(”bprofile_baby_name_div”).style.display=”block”;
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//alert(”Please enter a valid date”)
document.getElementById(”bprofile_baby_name_div”).innerHTML=”Please select a valid date.”;
document.getElementById(”bprofile_baby_name_div”).style.display=”block”;
return false
}
return true
}
USE
function UserVaccinRecord(){
if( isDate( document.getElementById(’vaccin_date’).value )== false ){
return false
} else {
document.getElementById(”alert_date_div”).style.display=”none”;
}
}
JavaScript Time Validation
function IsValidTime(timeStr) {
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
var matchArray = timeStr.match(timePat);
if (matchArray == null) {
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];
if (second==”") { second = null; }
if (ampm==”") { ampm = null }
if (hour < 0 || hour > 23) {
return false;
}
if (hour <= 12 && ampm == null) {
return false;
}
if (hour > 12 && ampm != null) {
return false;
}
if (minute<0 || minute > 59) {
return false;
}
if (second != null && (second < 0 || second > 59)) {
return false;
}
return true;
}
USE
function UserBirthRecord(){
if(!IsValidTime($F(’bbirthrecord_birth_time’))){
document.getElementById(’bbirthrecord_birth_weight_div’).innerHTML = “Time of Birth should be (HH:MM:SS AM/PM format).”;
document.getElementById(’bbirthrecord_birth_weight_div’).style.display=”block”;
document.getElementById(’bbirthrecord_birth_time’).focus();
return false;
} else {
document.getElementById(’bbirthrecord_birth_weight_div’).style.display=”none”;
}
}
JavaScript Trim Validation
function LTrim( value ) {
var re = /\s*((\S+\s*)*)/;
return value.replace(re, “$1″);
}
// Removes ending whitespaces
function RTrim( value ) {
var re = /((\s*\S+)*)\s*/;
return value.replace(re, “$1″);
}
// Removes leading and ending whitespaces
function trim( value ) {
return LTrim(RTrim(value));
}
USE
function ProjectValidation(){
var ProjectName = trim(document.frm.ProjectName.value);
if(ProjectName==”") {
document.getElementById(’Project_Div’).innerHTML = “Please enter project name”;
document.getElementById(’Project_Div’).style.display=”block”;
document.frm.ProjectName.focus();
return false;
}
}
Auto refresh parent window after closing popup
<script language=”JavaScript”>
<!–
function refreshParent() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow)
{
window.opener.progressWindow.close()
}
window.close();
}
//–>
</script>
Next Page »