3

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.

2
  • From where you want to get an element by it's id? In PHP. Commented Mar 21, 2010 at 6:35
  • Sorry. I suppose I left out a pretty crucial part. Please see my edit. Commented Mar 21, 2010 at 6:40

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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"? :)
That is interesting. Also, it looks like you have enough rep. now Aristotle (you should have gotten some rep for that name.) Ha.
2

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 ...

7 Comments

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).
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, 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.
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, 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.
|

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.