0

Is there any project like PHP.js but in vice direction to provide PHP implementation of JS classes and functions? In particular Date, RegEx, String classes?

I found this class for String but I am looking for a more complete collection.

My consern is not about using or not using such thing, I just need such thing.

Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173
  • 1
    Why would you need that? There are already regex, date and string functions built into php. – Thomas Glaser Nov 23 '12 at 10:06
  • In my opinion it's not a good approach, would just mess up the coding, specially if you are working on team, and on the updates. I think is not so hard to just learn the different behaviours of both scripts. But just my opinion, maybe im wrong xD – aleation Nov 23 '12 at 10:13
  • @jpo Because PHP dose not have such modern classes for such things, beside that using JS style of classes is more enjoyable. `:)` – Handsome Nerd Nov 23 '12 at 10:13
  • There's at least the [DateTime](http://www.php.net/manual/en/class.datetime.php) class. I'm afraid you would have to write the string and regex class yourself. (If you really, really need that) – Thomas Glaser Nov 23 '12 at 10:20
  • @jpo Thanks for your comment, I know that such class already exist, but while I am coding JS and PHP in parallel and such implementation classes have no noticeable performance penalty why should I use different functions for same thing. – Handsome Nerd Nov 23 '12 at 13:02

2 Answers2

0

Today I found jsphp. It seems promising.

JavaScript for PHP (jsphp) is a pseudo-implementation of the ECMA 262 standard (JavaScript 8.5.1) for PHP 5.3+. It allows you to write code in PHP as if it were JavaScript, using the standard API and the dynamic attributes available in the language, such as prototype inheritence and chaining, first-class functions, and other object-orientated features. It includes JSBoolean, JSNumber, JSString, JSObject, JSArray, JSFunction, JSRegExp, JSDate, JSError and JSMath, as well as the global helper functions, such as parseInt or isNaN. The syntax in jsphp is very similar to a native implementation, although adapted to the syntax of PHP. For example, the variables are prepended by a "$", the Object access opertaor is "->", and Strings are concatenated using a ".".

  $myString = new JSString( 'Hello World' );

  $myString = $myString->split( '' )->reverse()->join( '' );

  print( 'Reversed: ' . $myString ); // dlroW olleH
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173
-1

There is not - and there can not be - somehting like you ask for.

Javascript has a special syntax for regular expressions, something PHP could not take.

Similar how "object" methods in javascript are invoked. And the scope of variables is different. So this would not work.

Instead use the PHP functions. If they are not complete or useable enough for you, wrap them into objects so you can create an interface you like. Or use one of the many libraries that are available.

hakre
  • 193,403
  • 52
  • 435
  • 836