Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Thursday 12 December 2013

Hello World Program Using OOP in PHP

Following Script illustrates simple Object Oriented Programming in PHP. Script contains one class named "HelloWorld". Class contains variable named "world" and method/function named "getHtml()" to return message "Hello, World!". To call "getHtml()" method first we have to create object (in our example $greetings) of HelloWorld class. And using that object we are calling getHtml() method ($greetings->getHtml()).


Output:
Hello, World!

Source Code:

<?php

class HelloWorld 
{
    public $world = 'World';

    function getHtml() 
    {
     return "<html><body>"."Hello, ".$this->world."!"."</body></html>";
    }
}

$greetings = new HelloWorld;
echo $greetings->getHtml();

?> 

0 comments :

Post a Comment