-2

I am coming from the Ruby world and I have a PHP project I currently work on.

Like in Ruby scripts, is it possible to declare class methods in PHP? Basically, I'm asking what the equivalent of the following code would be in PHP

class A
  def hello; "hello from object"; end
  def self.hello; "hello from class"; end
end

Note the difference between the instance method and the class method.

Igbanam
  • 5,904
  • 5
  • 44
  • 68
  • 2
    A Google search for "PHP class" will definitely point you to the PHP manual, in particular the [Classes and objects](http://php.net/manual/en/language.oop5.php) page! – nico Jul 28 '12 at 07:21
  • 2
    I would refer you http://www.killerphp.com/tutorials/object-oriented-php/ if you are new to php. – Vinay Jul 28 '12 at 07:23
  • I have read through the PHP docs in detail. Didn't seen to find anything to help – Igbanam Jul 28 '12 at 07:24
  • Thanks Vinay. I'll check that out now – Igbanam Jul 28 '12 at 07:30
  • @Yasky Did you miss this page in your reading: http://php.net/manual/en/language.oop5.static.php – Bailey Parker Jul 28 '12 at 07:44
  • I probably did ( ._.) Moreso, I didn't realize static methods are the equivalent of class methods { ._.} – Igbanam Jul 28 '12 at 17:24

4 Answers4

1
class A
  def hello; "hello from object"; end
  def self.hello; "hello from class"; end
end

class A {
    // attributes or properties
    public $age; 
    private $gender; 
    protected $location; 
    // needs to be static to be called as self:: inside the class
    public static function hello(){
         return "hello from object";
    }
    // use this keyword to be called inside the class
    public function hello1(){
        return "hello from object";
    }
    public function hello2(){
         print(self::hello());
         print(this->hello1());
    }
    // How about private method
    private function hello3(){
         return "hello world";
    }
}

Calling Outside the class

$instance = new A();
//static
$instance::hello();
//non static
$instance->hello1();
$instance->hello2();
Johndave Decano
  • 2,101
  • 2
  • 16
  • 16
1

In PHP class methods are usually referred to as static methods and are declared as such. To directly translate your example:-

class A
{
    public function hello()
    {
        return "hello from object";
    }

    //We can't have two methods with the same name.
    public static function s_hello()
    {
        return "hello from class";
    }
}

Then you would call the methods like this:-

//For a static method we don't need an instance
echo A::s_hello;
//But we do for an instance method
$a = new A();
echo $a->hello();

You can also have static properties, so the above example could be modified something like this:-

class A
{
    private static $s_hello = "hello from class";
    private $hello = "hello from object";

    public function hello()
    {
        return $this->hello;
    }

    //We can't have two methods with the same name.
    public static function s_hello()
    {
        return self::$hello;
    }
}

http://php.net/manual/en/language.oop5.static.php

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • Thanks for the explanation. I would suggest reading JohnDave's answer too. Both are very well written. Thanks guys! – Igbanam Jul 28 '12 at 17:28
0

Look at static methods: http://php.net/static

A static method is called like that:

Foo::aStaticMethod();
mdo
  • 6,961
  • 3
  • 24
  • 26
  • Without additional code within your static method it will not return different results depending on where it is called from. – martynthewolf Jul 28 '12 at 07:39
  • @sabre: I didn't understand that his goal was to have two methods with the same name but only to know how to implement static methods. – mdo Jul 28 '12 at 07:48
-2

There is no way in php to have multiple definition of the same function in single class

rather you can do this

abstract class A{
 static function hello()
 {
   return 'hello';
 }
}

class B extends class A  
{
  function hello()
  {
  return 'hello from Object';
  }
}

A::hello();
$t = new B();
$t->hello();
Rupesh Patel
  • 3,015
  • 5
  • 28
  • 49