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 8509435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:26:02+00:00 2026-06-11T03:26:02+00:00

I write some class to work with string like in C#. Here it is:

  • 0

I write some class to work with string like in C#.

Here it is:

class String {

        private $origin_string = null;
        private $result_string = null;

        function __construct($string)
        {
            $this->origin_string = $string;
            $this->result_string = $this->origin_string;
        }

        public function Trim()
        {
            $this->result_string = Trim($this->result_string);
            return $this->result_string;
        }

        public function StartWith($string)
        {
            return (substr($this->result_string, 0, strlen($string)) === $string);
        }

        public function EndWith($string)
        {
            $endlen = strlen($string);
            $strlen = strlen($this->result_string);
            return (substr($this->result_string, $strlen - $endlen, $endlen) === $string);
        }

        public function Contains($string) {
            return (strpos($this->result_string, $string) !== false);
        }

        public function Replace($search, $string) {
            $this->result_string = str_replace($search, $string, $this->result_string);
            return $this->result_string;
        }

        public function __invoke($string) {
            $this->origin_string = $string;
            $this->result_string = $this->origin_string;
            return $this;
        }

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

        public static function Override($string)
        {
            return new self($string);
        }

    }

In use:

$s = new String("My custom string");

if ($s->StartWith("My"))
    $s->Replace("custom", "super");

print $s; // "My super string"

To correct print my text from object i use magic method __toString().

Question:
Is there a method, the inverse __toString?
That is so we can write:

$s = "new text";

And the line is assigned to variables within the object.

($s – an existing object “String” in the example above.)

An analogue of the method __set, only in relation to the object, not the variable inside it.

While using the __invoke, but it’s not quite what I want.

  • 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-11T03:26:03+00:00Added an answer on June 11, 2026 at 3:26 am

    No.

    $s = "new text"; assigns the (native PHP) string "new text" to the variable $s. It overwrites whatever $s was before. It does not call any methods on $s if $s is an object.

    You’d have to alter the core behavior of PHP to achieve something like that. You’ll always have to explicitly call a method on your String object.

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

Sidebar

Related Questions

Here's a really simple class: static public class Bean1 { final private String name;
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
i write some utility class but how to get name ? Can that send
I'm trying to use the ofstream class to write some stuff to a file,
Whenever I write a new class and add some internal fields, I use an
I want to interrupt some specific grails domain class events(read,write,delete,update).Is there any hibernate eventlistner
As part of some WSGI middleware I want to write a python class that
I am using Qt and wish to write a class that will perform some
I would like to write a program to receive some data using tcpClient from
Lets say I have this amputated Person class: class Person { public int Age

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.