6

Seeing other languages like Java or C++, String is an Object.
But in PHP is it the same thing?

4
  • PHP supports OOP, but is not fundamentally oop-based. strings are not objects in PHP. Commented Mar 28, 2013 at 16:11
  • You can actually have a string object in PHP with the SPL Types: php.net/manual/en/class.splstring.php ... but this is very new and very buggy Commented Mar 28, 2013 at 16:23
  • It's not natively an object but you could create your own string class if you wished. Just a few days ago I was thinking about this. Commented Mar 28, 2013 at 16:30
  • Is it the literal "String"? Or just a string? Commented Nov 14, 2023 at 15:07

6 Answers 6

11

A String is not an object in PHP by default and casting is not required but it can be introduced if you want using scalar_objects

class StringHandler {

    public function length() {
        return strlen($this);
    }
}

register_primitive_type_handler('string', 'StringHandler');

So you can easily have

$str->length();
Sign up to request clarification or add additional context in comments.

Comments

5

A string is not an Object in PHP. You don't have to cast the types of variables in PHP. See http://www.php.net/manual/language.types.string.php and http://php.net/manual/language.types.type-juggling.php

4 Comments

Ok so a string is a string I guess.
$name = "John"; makes a String. PHP is much simpler than C(++) in this case. It tries to figure out what type your suggest.
So I need to write it string and not String. Sorry I like the conventions :-)
If you want you can write $name = (string)"John"; or $number = (int)"99";
3

String and object are two different things. But if you wanted to run your own checks:

if (is_object($var))
{
 echo "Var is an object"; 
}
elseif (is_string($var))
{
 echo "var is a String"; 
}
else
{
  echo "var is neither an object or string"; 
}

3 Comments

I already did this test, but I wasn't sure if they just replaced the object kind by a string kind which it could had been itself an Object... That's the purpose of my question :-)
@user1682624 As a majority of the answers have stated, a string is not an object.. the same concept with an object being a string, the other answers provided are your solution
This isn't really an accurate answer.
3

String is not an object in PHP; it's a primitive type. The information is on Types.

A lowercase string is correct for the few uses you might have.

2 Comments

What do you mean by the last sentence?
OK, the OP has left the building: "Last seen more than 2 years ago"
0

Everyting can be achieved via the built-in functions of PHP, so you can do all that you can with String objects.

Comments

0

A string is not an object; it is a basic type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.