tags:

views:

55

answers:

2

Are there any libraries or tools specifically designed to help PHP programmers write Javascript? Essentially, converting the PHP logic into Javascript logic. For instance:

$document = new Document($html);
$myFoo = $document->getElementById("foo");
$myFoo->value = "Hello World";

Being converted into the following output:

var myFoo = document.getElementById("foo");
myFoo.value = "Hello World";

So the $html that is passed in won't initially be modified by the PHP. Instead, the PHP will convert itself into Javascript which is then appended onto the end of the $html variable to be ran when the variable it output and converted into the client-side DOM.

Of course it would be excellent if more complicated solutions could be derived too, perhaps converting objects and internal methods into javascript-objects, etc.

+1  A: 

Why would you want to do this?

Generally you want to do in PHP things you want to run on the server, and in JS things you want to do on the client (user's) machine.

However if you really want to, check out this http://phpjs.org/

php.js is an open source project that brings high-level PHP functions to low-level JavaScript platforms such as web browsers ...

Justin Jenkins
I'm familiar with the phpjs project, but it's not what I'm looking for (that's for people who are *writing javascript*. I'm writing PHP) I want to output dynamically-written Javascript much like you can do with .NET (If I'm not mistaken).
Aristotle
I deal mostly with .NET ... if you can give me a better idea of what you are expecting maybe I can help. Do you want to convert PHP to JS, or dynamically output JS?
Justin Jenkins
Justin, I'm not sure I can put it any differently than I did in my question. I don't want the PHP to manipulate the `$html`. Instead, I want it to output my logic converted into Javascript, and appended onto the `$html`. This way, when the client receives the end-result, the dynamically-added Javascript will perform the logic that previously existed as PHP code.
Aristotle
I'm not sure if I'm following but for others that might be ... are you referring to how ASP.NET for example will auto generate JS to do validation on a form field ... example: to see if you entered a date in a date text box?
Justin Jenkins
Justin, yes, along those lines. Only I don't want to be doing any postbacks. I'd like all of the logic to bind events to be present once the page is loaded.
Aristotle
Ah, I think I know exactly what you are saying. However I have no idea how to do that in PHP, sorry. ASP.NET can do this because in Visual Studio you basically use a wizard to configure your "validation" and it auto generates the JS for you ... BTW it's not always a postback anymore ... I wish I could be of more help but maybe you can edit your question so others can understand what you are wanting to do. Good luck.
Justin Jenkins
Thank you, Justin. I appreciate your patience.
Aristotle
+4  A: 

Sounds like you may be looking for something like http://xajaxproject.org. However, it might be geared more towards ajax functionality.. not all javascript. I toyed with it about 2 years ago but never used it seriously. Might be worth a look.

Mike B
This is definitely along the lines of what I'm looking for. As you said, it's a little specific to ajax, but neat none-the-less. I would upvote but I don't have enough rep. Will you settle for a hearty "Thanks"? :)
Aristotle
That is interesting. Also, it looks like you have enough rep. now Aristotle (you should have gotten some rep for that name.) Ha.
Justin Jenkins
I appear to have enough _now_. Upvoted!
Aristotle