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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:55:57+00:00 2026-05-13T22:55:57+00:00

Hai. I was making this simple string class and was wondering if there was

  • 0

Hai. I was making this simple string class and was wondering if there was a more natural way of doing it.

class Str{
    function __construct($str){
        $this->value = $str;
        $this->length = strlen($str);
        ..
    }

    function __toString(){
        return $this->value;
    }
    ..
}

so now i have to use it like this:

$str = new Str('hello kitty');
echo $str;

But that doesnt look very ‘natural’ with the parentheses. So i was wondering if something like this, or similar was possible.

$str = new Str 'hello kitty'; # I dont believe this is possible although this is preferred.

$str = new Str; # get rid of the construct param.
$str = 'value here'; #instead of resetting, set 'value here' to Str::$value??

In the second method, is there a way i could possibly catch that variable bing set again and instead of reseting it, set this to Str::$value ? I have thought around and the closest i could come up to is the __destruct method. but there was no possible way to know how it was being destroyed. Is this possible or am i wasting my time?

  • 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-13T22:55:57+00:00Added an answer on May 13, 2026 at 10:55 pm

    Since PHP is loosely typed, there is no natural string class, because the natural way of using strings is, well, by just using them. However, as of PHP5.3 there is an extension in the SPL that provides strongly typed scalars:

    • http://php.net/manual/en/book.spl-types.php

    However, keep in mind that this extension is marked experimental and subject to change. As of now, it is also not available on Windows.

    You might also want to try https://github.com/nikic/scalar_objects

    This extension implements the ability to register a class that handles the method calls to a certain primitive type (string, array, …). As such it allows implementing APIs like $str->length().

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

Sidebar

Related Questions

Hai I am doing a web page. In that there is a drop down
public enum myEnum { VAL1(10), VAL2(20), VAL3(hai) { public Object getValue() { return this.strVal;
I have used this in my HTML: <q> Hai How r u </q> Which
Hai. I've got site which is really weird, and now is making me troubles.
Hai Guys, Like DataAccessApplicationBlock for .net with sql server,Is there any thing for MySql
hai Friends I am using this coding for validating the emailID online through webservice
hai friends I am one dataset like this ds like three columns this: tblkey
Hai Freinds So far i have used this coding to create a custom Control
Hai, I am using Java GWT. In this i want to load one html
Hai all, I am doing c#.NET application to calculate different formulas and give result

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.