Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:03:10+00:00 2026-06-08T22:03:10+00:00

I am coming from the Ruby world and I have a PHP project I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:03:13+00:00Added an answer on June 8, 2026 at 10:03 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am coming from the world of PHP and am learning Ruby/Rails. I have
Please note I'm coming from a Ruby & Node.js world and would like to
I'm coming from a PHP world and pretty new to Ruby, so there may
I'm brand new to java, coming from a ruby world. One thing I love
I have recently started learning C++ and coming from a Ruby environment I have
Coming from the ruby on rails world, I'm looking for an efficient way to
Coming from java/groovy/grails we are currently moving to rails. We have been having an
Say I have the following png In my HTML, coming from Ruby I get
I am coming from Java to Ruby and this -7 mod 3 = 2
Coming from a PHP background, I'm used to writing small functions that return a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.