How to achieve and use Polymorphism in PHP

Polymorphism – the concept

 

Polymorphism is a Greek word that means “many forms”. In object-oriented programming, polymorphism is a mechanism by which objects of different types can process data through a single interface. Using a typical illustrative example, a program might define a type of object called Animal, containing a function called talk() and, through inheritance, derive object sub-types such as Cat, Dog, etc. that each have their own specific implementation of the talk() function. The mechanism of polymorphism allows such a program to call the talk() function without actually knowing whether it is calling it for a Cat, Dog, or any other derivative Animal, while executing the correct function for the specific type of Animal.

 

Why method polymorphism cannot be achieved in PHP?
In languages like Java where in a function sum (int, int) differs from sum (float, float), the method sum() has many forms depending on the parameters being passed to it.

But in PHP it is not possible because you can have a method that accepts two parameters and call it by passing three parameters (allows variable arguments). This is because PHP is not strict and contains methods like func_num_args() and func_get_arg() to find the number of arguments passed and get a particular parameter.

 

Polymorphism is not the same as method overloading or method overriding (in OOP, a method is a function that belongs to a class, while the class variables are referred to as its members) and should not be confused with these.

 

Method overloading is where a class has two or more functions of the same name, but accepting a different number and/or data types of parameters.

 

Method overriding is where a child class (one derived through inheritance) defines a function with the same name and parameters as a function in its parent class, but provides a different implementation of the function. This is not the same as polymorphism, as it does not necessarily involve late-binding, however overriding is part of the mechanism by which polymorphism is accomplished.

 

 

PHP 5 & Polymorphism
Since PHP 5 introduces the concept of Type Hinting, polymorphism is possible with class methods. The basis of polymorphism is Inheritance and overridden methods.

[php]

<?php

class BaseClass {
public function myMethod() {
echo "BaseClass method called";
}
}

class DerivedClass extends BaseClass {
public function myMethod() {
echo "DerivedClass method called";
}
}

function processClass(BaseClass $c) {
$c->myMethod();
}

$c = new DerivedClass();
processClass($c);

?>

[/php]

 

OUTPUT:

DerivedClass method called

 

In the above example, object $c of class DerievedClass is executed and passed to the processClass() method. The parameter accepted in processClass() is that of BaseClass. Within the processClass() the method myMethod() is being called. Since the method is being called on the class variable of BaseClass, it would not be wrong to assume that myMethod() of class BaseClass will be called. But, as per the definition “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism”, myMethod() will be called on object DerievedClass. The reason why this happens is because the object of DerievedClass is being passed and hence the method myMethod() of DerievedClass will be called.

 

I hope the above makes sense and hasn’t been too confusing as you know this is difficult to explain in clear English ūüôĀ

 

Can not receive emails sent from my mail server to my test accounts on gmail, hotmail and yahoo, how to resolve this issue?

On a normal server, there is a limit of mails, that can be sent in a day, but due to error or some issue if a large number of mails sent automatically from your server, and then they (gmail, hotmail yahoo and other) block the incoming mails from that server.

 

This means that your IP address is blocked and reported as a spam IP, to remove it from the spam list,

 

1. Go to http://www.spamhaus.org/lookup.lasso
2. Check your IP/Domain if it is blocked
3. If it is blocked select to remove it from black list

 

Wait between half and one hour and then try again to send an email to hotmail, gmail or yahoo.

 

 

Share your WordPress page URL on social networking website by using a simple function

Share your WordPress post on facebook & other social networking website.

 

If you want to share your WordPress page URL on social networking website, it’s very simple to do.

Just place this function in your functions.php file, and call the function where do you want it to show.

 

[php]
<?php

function GetShareLinks() {

/* Your current page URL */
$URI = get_permalink();

/* The js script used for goolge plus share button */
$sharestring ='<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>’;

$sharestring .='<a href="https://pinterest.com/scriptarticle/" target="_blank"><img width="16" height="16" alt="Follow Me on Pinterest" src="http://passets-lt.pinterest.com/images/about/buttons/small-p-button.png"></a>
<a href="http://www.facebook.com/sharer.php?u=’.$URI.’"><img src="’.get_bloginfo(‘template_url’).’/images/facebook.jpg" alt="" /></a>
<a href="http://twitter.com/intent/tweet?text=’.$URI.’"><img src="’.get_bloginfo(‘template_url’).’/images/twitter.jpg" alt="" /></a>
<a href="http://www.linkedin.com/shareArticle?mini=true&url=’.$URI.’"><img src="’.get_bloginfo(‘template_url’).’/images/in.jpg" alt="" /></a>
<g:plusone count="false" size="small"></g:plusone>’;

return $sharestring;

}

?>
[/php]

 

You can also use this function in other website (non WordPress) site, you have to just replace the get_permalink() function by current page URL and update the social icons images path.

 

If you face any issue to use this, put your comment, I’ll help you ūüôā

 

Display Recent Blog Posts Outside WordPress

Generally web master uses the WordPress for applying blog on their website.

 

Below is the code for the getting recent/latest post or list, can be applied to display recent posts outside anywhere in your WordPress website.

 

The key to use WordPress outside is include the wp-load.php file
Once the wp-load.php file is included, the entire wealth of WordPress functions will available for you to use.
This can be found under the root folder.

[php]
<?php

require_once ‘wp-load.php’;

// Recent 10 posts
$recent_posts = wp_get_recent_posts(array(‘numberposts’ => 10));

// Apply your design/html
echo ‘<ul>’;
foreach($recent_posts as $post) {
echo ‘<li><a href="’, get_permalink($post[‘ID’]), ‘">’, $post[‘post_title’], ‘</a></li>’;
}
echo ‘</ul>’;

?>
[/php]

If you want to pass more argument, you can do, below is the WordPress reference URL for wp_get_recent_posts function
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts

 

You can also do your own custom queries with WP_Query.

 

Get a separate RSS feed for custom post type WordPress

rss feed for custom post type

Everyone is utilizing custom post types in their new WordPress sites as it is a very powerful and popular feature now days, now the next one is make a separate RSS feed for custom post type?

 

How can a user create a separate RSS feed for a specific custom post type in WordPress?

 

It’s so simple,it works just like creating a separate RSS feed for each category in WordPress.

 

You can simply go by typing this URL.

 

http://www.yourwebsite.com/feed/?post_type=location

 

Just change the post_type name to the one that you want to create a separate RSS feed for.

Are you looking to take it to the next level and make it for a specific custom post type and custom taxonomy? Well that is also not so hard at all.

 

http://www.yourwebsite.com/feed/?post_type=location&genre=newyork

 

Here genre would be the custom taxonomy, and new york would be the tag if you may call it that.
You can create a button in your site sidebar to link to that.You can also link this feed and create a separate feed burner feed for it.

 

For grabbing most popular WordPress custom post type plug-in,click on below URL.

 

http://wp-types.com/

 

 

Most useful 5 htaccess tricks every webmaster should know

Most useful 5 htaccess tricks every webmaster should know

1) Redirect your website visitors while you update or test your website

order deny,allow
deny from all
allow from 117.117.117.117

ErrorDocument 403 /showpage.html

<Files showpage.html>
allow from all
</Files>

Replace 117.117.117.117 with your IP address. Also replace showpage.html with the name of the page you want visitors to see.

2) Display a custom 404 error page

Your server displays a “404 File Not Found” error page whenever a visitor tries to access a page on your site that doesn’t exist.
You can replace the server’s default error page with one of your own that explains the error and links visitors to your home page.

ErrorDocument 404 /404.html

Replace 404.html with the name of the page you want visitors to see.

3) Handle moved or renamed pages

You’ve moved or renamed a page on your site and you want visitors automatically sent to the new page when they try to access the old one.

Use a 301 redirect

Redirect 301 /oldpage.html http://yourwebsite.com/newpage.html

Using a 301 redirect also ensures the page doesn’t lose its search engine ranking, as you know how important it is.

4) Prevent directory browsing

When there’s no index page in a directory, visitors can look and see what’s inside (directory structure and page listing).
Some servers are configured to prevent directory browsing like this. If yours isn’t, here’s how to set it up:

Options All -Indexes

5) Create user friendly URLs

Which of the two URLs below looks good?

http://yourwebsite.com/about
http://yourwebsite.com/pages/aboutus.html

Shorter URL is always better.

With htaccess and an Apache module called mod_rewrite, you can set up URLs however you want. Your server can show the contents of “/pages/aboutus.html” whenever anyone visits “http://yourwebsite.com/about”. Below are few examples

RewriteEngine on
RewriteRule ^aboutus/$ /pages/aboutus.html [L]
RewriteRule ^features/$ /features.php [L]
RewriteRule ^buy/$ /buy.html [L]
RewriteRule ^contactus/$ /pages/contactus.htm [L]