Jump to content

[SOLVED] PHP and Javascript: Just How Similar are they?


Fluoresce

Recommended Posts

I've recently started learning JavaScript. I already know a bit of PHP. I've noticed that the two languages are very similar. This is strange because I've never heard anybody say that they are similar. I'm assuming that this is because they become increasingly disimilar as you learn more? Is that correct?

Link to comment
Share on other sites

I've recently started learning JavaScript. I already know a bit of PHP. I've noticed that the two languages are very similar. This is strange because I've never heard anybody say that they are similar. I'm assuming that this is because they become increasingly disimilar as you learn more? Is that correct?

 

Like Mchl said, their similarities lie in their syntax.  Other languages have a similar syntax as well (C, C++, C#, Java, Perl, etc.).

 

The two are completely different, otherwise.  PHP is a server-side language.  Therefore, it can't read and respond to browser events (button presses, mouse movement or clicks, etc.) directly.  JavaScript is client-side.  It runs in the browser and allows sites to be more interactive by responding to the kinds of browser events I mentioned earlier.

Link to comment
Share on other sites

You say strange things... A 'class' is a core concept in OOP. Can't have OOP without classes...

 

I think what you mean, is that JS is much more object oriented than PHP. Just about everything in JS script is an object (including variables). This is approach taken from Java (hence the name).

Link to comment
Share on other sites

function Person (name, age) {
     this.name = name;
     this.age = age;
}

Person.prototype.getName = function () { return this.name; }
Person.prototype.getAge = function () { return this.age; }

var Maq = new Person("Maq", 17); // just putting some age
alert(Maq.getName());
alert(Maq.getAge());

 

That good enough or should I keep going?

Link to comment
Share on other sites

The fact that you don't have to use 'class' keyword to define a class in JS, does not mean there are no classes...

 

I'll ask the other way round: What this piece of code you posted does?

Link to comment
Share on other sites

It alerts a string followed by another alert of a number.

 

The point was to illustrate the difference between PHP and JavaScript. There isn't this class thing where you can just put all your variables and functions in. Logically, you can view it as a class. It just isn't to me. In the end, it's still a function that you can use and call without making it OOP. I just didn't expand on the function to have it do that.

Link to comment
Share on other sites

I do view a class as such because it looks exactly like that. It has some variables and some functions. Breaking it down, that's pretty much all you see in a class.

 

Of course the idea behind it and its usage is different.

Link to comment
Share on other sites

I meant this piece of code

function Person (name, age) {
     this.name = name;
     this.age = age;
}

Person.prototype.getName = function () { return this.name; }
Person.prototype.getAge = function () { return this.age; }

 

Ok, so you'll say 'defines a function and two prototypes'. But what's the point? The point is to describe a certain class of objects you can create and use later.

 

A class is fundamental concept of object oriented programming. JavaScript just has a bit wicked way of declaring them.

 

 

[edit] Actually also wicked way of making use of them.

Link to comment
Share on other sites

Yeah ECMA-262 says:

ECMAScript does not contain proper classes such as those in C++' date=' Smalltalk, or Java, but rather, supports constructors which create objects by executing code that allocates storage for the objects and initialises all or part of them by assigning initial values to their properties.[/quote']

 

However, the same document refers to an internal property of all objects called Class numerous times. See e.g. p. 39:

[[Class]]: A string value indicating the kind of this object.

 

As far as I'm concerned, Javascript does operate with classes though they decided to call it prototypes instead.

Link to comment
Share on other sites

Technically that's correct. JavaScript uses concept of prototypes, which differ in behaviour from classes as known from PHP or Java. (and described by me as 'wicked' :P)

 

However the goal of both a 'class' and a 'prototype' is ultimately the same. On higher level of abstraction they are essentially the same.

Link to comment
Share on other sites

What the . . . !  :o

 

I thought, let me take a quick look to see if anyone's responded to my thread. Instead of finding a response, I found two pages of arguments!

 

Interesting cases, guys.

 

Yes, seems as if we semi hi-jacked your thread in a brief debate, but hopefully you've learned a thing or two from thread, I have.

 

That's exactly right. I can see the concept of classes in JavaScript, but they're not classes in my POV.

 

I guess we can all agree that you need the concept of 'classes' in order to achieve OOP.  ;D

Link to comment
Share on other sites

What the . . . !  :o

 

I thought, let me take a quick look to see if anyone's responded to my thread. Instead of finding a response, I found two pages of arguments!

 

Interesting cases, guys.

 

Yes, seems as if we semi hi-jacked your thread in a brief debate...

 

This would be akin to you flying to a specific destination, only to have the pilots (during autopilot mid flight) start getting into a debate about who's wife is better and realize after some time that they have over shot their destination, and with the amount of fuel they have left, are forced to land somewhere else.

 

Thank you for flying 'OOP? Airways'. We apologize for not getting you to your destination. It appears our flying methods were overridden. We promise to have better class next time  :-\

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.