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

  • SEARCH
  • Home
  • 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 7160611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:25:38+00:00 2026-05-28T13:25:38+00:00

Im relatively new to PHP but have realized it is a powerfull tool. So

  • 0

Im relatively new to PHP but have realized it is a powerfull tool.
So excuse my ignorance here.

I want to create a set of objects with default functions.

So rather than calling a function in the class we can just output the class/object variable and it could execute the default function i.e toString() method.

The Question:
Is there a way of defining a default function in a class ?

Example

class String {
     public function __construct() {  }

     //This I want to be the default function
     public function toString() {  }

}

Usage

$str = new String(...);
print($str); //executes toString()
  • 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-05-28T13:25:38+00:00Added an answer on May 28, 2026 at 1:25 pm

    There is no such thing as a default function but there are magic methods for classes which can be triggered automatically in certain circumstances. In your case you are looking for __toString()

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

    Example from Manual:

    // Declare a simple class
    class TestClass
    {
        public $foo;
    
        public function __construct($foo)
        {
            $this->foo = $foo;
        }
    
        public function __toString()
        {
            return $this->foo;
        }
    }
    
    $class = new TestClass('Hello');
    echo $class;
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I'm relatively new to php but have worked on web apps previously. I'm
I am relatively new to PHP, but experienced Java programmer in complex enterprise environments
This may be a silly question, but as someone relatively new to PHP, I'm
I am relatively new to MySQL and PHP and I have been trying to
I'm relatively new at PHP and came across a slight problem. I have a
I am relatively new to PHP/MySQL and have been having a problem that I
I am relatively new to mod_rewrite, but have a site which I would like
I'm relatively new with PHP and more so with XML. I have a script
I'm relatively new to web programming. New to Javascript, PHP, Ajax, etc. I have
I'm relatively new to PHP and have exhausted the internet trying to find an

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.