开发者

Object orientated strings / numbers in PHP?

开发者 https://www.devze.com 2023-01-29 23:48 出处:网络
I was looking at Ruby and it has a very nice OO structure unlike PHP with C-like string functions. I was wondering if there is an extension which makes strings into objects so you could use them like

I was looking at Ruby and it has a very nice OO structure unlike PHP with C-like string functions. I was wondering if there is an extension which makes strings into objects so you could use them like this:

$str = "sometext";
echo "len:" . $str-&g开发者_如何学Ct;length; //would print 'len: 8'


Take a look at this...

http://code.google.com/p/php-string/downloads/detail?name=string.php&can=2&q=

The class supports the extensions mbstring and iconv, and the package PHP-UTF8. It chooses the best available function for each method In addition, it provides many new methods. Some of them are: substringBetween, splice, startWith, endsWith and squeeze. It is also possible to use PHP internal functions, or custom functions, to manipulate the string.

Sample Code:

<?php

include('string.php');

$str = new String('sometext');
echo $str->length; //prints 8
echo $str->getLength(); //prints 8

?>

I have never used this class before but by looking at its documentation it has some pretty interesting methods. capitalize, charAt, compareTo, contains, etc..


I'm a bit late to the game, but I was looking for a library just like this and came across this question. After more investigation I found the brilliant danielstjules/Stringy at GitHub.

I've looked over the documentation and the source and it looks pretty damn solid. I'd recommend taking a look at if you want a PHP String Wrapper class to make string manipulation easier. Note that this code is not a PHP extension, meaning there is no native manipulation, it is simply a wrapper.

Object orientated strings / numbers in PHP?

A PHP string manipulation library with multibyte support. Compatible with PHP 5.3+, PHP 7, and HHVM.

A few examples:

s('Upper Camel-Case')->upperCamelize(); // 'UpperCamelCase'
s('What are your plans today?')->truncate(19, '...'); // 'What are your pl...'
s('foo & bar')->containsAll(['foo', 'bar']); // true
s('Ο συγγραφέας είπε')->countSubstr('α'); // 2


If you wanted to you could create your own String wrapper class that has all the string based methods and calculated attributes that you could possibly want. Edit: In the same way that Java has wrapper classes for some data types.


While Jose Vega suggested a good solution for your problem, there is a farily minor performance issue with the approach. I've whipped up a test for this. While each solution performs well enough for practical purposes, there is a difference.

EDIT: So anyway, since this is neither Ruby, nor Java, nor Smalltalk, you'd be better off using the provided tools than trying to bend the language to meet your expectations. Mostly for performance reasons (because native tools are usually written in C or are low-level enough to have very little performance impact, although it's not always true), and for readability: the community is used to seeing mb_strlen() or strlen(), and other string-related functions.

Here's the code I used for benchmarking:

http://pastebin.com/Q4BfzQtj

Results:

====> Test run 0
And here are the results:
Test with {} = 0.00097203254699707
Test with strlen() = 0.0030488967895508
Test with mb_strlen() = 0.0031669139862061
Test with String1 object = 0.012485027313232
Test with String object = 0.036020040512085


====> Test run 1
Test with {} = 0.00095200538635254
Test with strlen() = 0.0029759407043457
Test with mb_strlen() = 0.0031669139862061
Test with String1 object = 0.012346982955933
Test with String object = 0.036028146743774


====> Test run 2
Test with {} = 0.0009617805480957
Test with strlen() = 0.0029959678649902
Test with mb_strlen() = 0.0031518936157227
Test with String1 object = 0.012416124343872
Test with String object = 0.037784099578857


====> Test run 3
Test with {} = 0.00081610679626465
Test with strlen() = 0.0025439262390137
Test with mb_strlen() = 0.0027410984039307
Test with String1 object = 0.010634183883667
Test with String object = 0.030903100967407


====> Test run 4
Test with {} = 0.00081205368041992
Test with strlen() = 0.0025379657745361
Test with mb_strlen() = 0.0027129650115967
Test with String1 object = 0.010583162307739
Test with String object = 0.031081914901733


====> Test run 5
Test with {} = 0.000823974609375
Test with strlen() = 0.0025639533996582
Test with mb_strlen() = 0.0026860237121582
Test with String1 object = 0.010586023330688
Test with String object = 0.030833959579468   


There is SplString. But it's not available in PHP 5.3 yet. And I doubt it's of much use if it comes around, since it harbors no useful methods whatsoever. Maybe one could built upon it. But then that's only for strings. PHP is built upon scalars, and using it fully object-oriented is not possible at this time.


It's 2014 and SplString is still not default in PHP. I'd take a look at:

https://github.com/nikic/scalar_objects

It's an extension, so you have to install it.

0

精彩评论

暂无评论...
验证码 换一张
取 消