• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2232
  • Last Modified:

OOP difference between Java and PHP

Hello

I read at different places that there is a difference at OOP between Java and PHP. Could you please explain and give a brief example of code?

Kind regards.
0
hasozduru
Asked:
hasozduru
  • 6
  • 3
  • 2
  • +1
4 Solutions
 
neesterCommented:
Hey,

OOP In PHP is pretty similar to Java.

The thing I dont like about PHP and OOP is that it undermines half the reason for having OOP.
Since every page reloads the whole OOP code, so you retain no information...
So that means any classes you built, and any prefereces you had for the attributes, you now need to reset.

Whereas in Java if a user makes changes, and clicks a button, the program remembers the decisions and the class settings.



Also OOP is proven to be the slowest method for PHP to compile.

I read it goes like this:

- OOP (slowest)
- Function Based (middle)
- Inline Code (fastest)

also im sure it really depends on what you are doing :)
0
 
neesterCommented:
as for php OOP code,
here is a GREAT website:

http://codewalkers.com/tutorials/54/1.html
0
 
neesterCommented:
Here is a little snippet from the site:

<?php
class Box
{
  var $contents;

  function Box($contents) {
    $this->contents = $contents;
  }

  function get_whats_inside() {
    return $this->contents;
  }
}

$mybox = new Box("Jack");
echo $mybox->get_whats_inside();
?>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
hasozduruAuthor Commented:
Neester, thanks for your answer and example. But I would like to learn difference between Java and PHP as an OOP. Do you know it?

Thanks
0
 
neesterCommented:
Hey hasozduru,

Well better the OOP syntax and method, there is no real differences...
Java itself is a much differnt language to PHP - but OOP wise, whatever you do in Java, im fairly sure you can do in PHP.

From the code I posted, you can see how PHP is similar to Java syntax wise.
Methodolgy wise, its also the same.
You simply setup a class, and have functions inside the class.
You can extend classes (covered in that tutorial) etc...

It would be easier if you asked specifically about a method or a block of code...

Glad to help mate :)
0
 
hasozduruAuthor Commented:
I read several books so far and every one of them tells that PHP is not fully OOP like Java and C++. I would like to know the difference please.

Thanks
0
 
neesterCommented:
I think the reason they say its not fully OOP, is like I said previously.
Because the whole idea behind OOP, is that you setup classes, and in Java you modify the classes, and when you modify one, they all change etc...
It is easy to change lots of settings at once etc...
The classes can be massive and contain a lot of information.

The issue here is PHP cannot retain information, because essentially its recompiled every time you load the page.
So teh whole OOP structure is thrown out, and rebuilt every time you refresh.
So it does lose 50% of its usefulness - the other 50% of its usefulness is the simplicity to read the code and make updates.


It would probably be best to wait on someone else to respond, maybe I have missed something in my learning.
Although i haven't remembered anything crucial thats missing from PHPs functionality.
0
 
matt_mcswainCommented:
>>tells that PHP is not fully OOP like Java and C++
php4 is certainly not, but php5 was a huge step forward. php4 is really just a skeleton of an OO language. Some would argue that it isn't OO at all. You can create classes w/ methods and properties and derive classes from other classes, but that's about it.
No abstract classes, no encapsulation(everything is public), no exception handling, no static properities, no real syntax overloading, etc. Fortunately php5 has all of those things and more, but I'm sure it's still not held in the OOP esteem of Java and C++, but it's only been out a year.

php5 doesn't allow multiple inheritance of classes, meaning:

class A extends B,C,D { //

is not legal. But then again, you can't do that in Java either, but you can in C++.
Java and php both use interfaces instead which is done like:

class A implements B,C,D {

Notice, one class can implement multiple others. But with an interface you can only share a method prototype instead of an actual method, and each class that shares the interface must define the method in its class definition.

php5 also added Java's "final" keyword for classes and class methods. If a class or class method is declared final, it can not be inherited from any further.

Remember, those are all php5 examples; there is a huge difference between for 4 and 5 when discussing OOP.
I can't really give you any Java syntax, b/c it's been awhile since I've used it, but I'd be happy to provide any further php examples.
0
 
neesterCommented:
Ahh see - I knew someone else would come in and clear this all up :)

I am not too familiar with C++ - only C, so I havn't done much OOP except for my Java experience.
:)

Thanks for helping out Matt :)
0
 
hasozduruAuthor Commented:
Hi matt_mcswain

Yes if you can please provide me some PHP5 OOP examples it will be great.

Thanks
0
 
matt_mcswainCommented:
php5 like java and C++ added the public, private, and protected access modifiers. A class member is public by default.
If a class member is declared public, it can be accessed anywhere within or outiside the class like:

class A {
   public $prop = 'Anybody can use me!';
   
   function aMethod() {
     $this->prop = 'foo';
  }
}

$obj = new A;
echo $obj->prop;

The above line will not work for private and protected members. They both need to be modified by a class method like aMethod does.  The difference between private and protected is protected are accessible to derived classes, while private are not.

Something that I really like are class properities. You declare a property static and it then belongs to the class itself rather than an instance of the class. It's good for keeping track of the instances of a class(like giving each a unique id) or when you want to only have one application wide instance of an object. Simple example:

class A {
  static $aID = 0;
  protected $instanceID;
  __construct() {
    self::$aID++;
    $this->instanceID = self::$aID;
  }
}

To access static variables you should use the scope resolution operator(::). "self" is used to reference a static property that owned by a class within the class itself. You can also use "parent" to do the same for a static property of an objects parent class. Static variables are alot like global variables and can be accessed outside the class like:

echo A::$aID;

Another one of my favorites of php5 is the magic function __autoload(). You can define an __autoload function that tells php where to find your classes, and you never have to worry about include()ing a class again!
Like:

function __autoload($className) {
  require_once $_SERVER['DOCUMENT_ROOT'] .'/includes/'. $className .'.php';
}

As long as follow that name convetion for your classes or any other you want, and of ofcourse include() the __autoload function, you can create any object you want and let php sort it out. :)

Another thing php took from Java is the clone keyword. To copy an object in php5 you should use clone like:

$obj = new A;
//do stuff do $obj
$obj2 = clone $obj;

Of course php is flexible, so you can define a __clone method within your object, to make any alterations you want during the copy. php will copy all properties by default and just make the changes that you specified in your __clone() method.

And this doesn't even scratch the surface. We could go on for days!
Check out this link, if you haven't already-->http://php.paradoxical.co.uk/manual/en/language.oop5.php
0
 
Marcus BointonCommented:
A couple of other differences I can think of. Java and C++ have namespaces (closest you can get in PHP5 is to use static methods) and operator overloading, for example + normally knows how to work on integers and floats, but you can extend it so that you could apply the + operator to classes you made up. Java also doesn't let you run non-OO code like PHP does - there are no globals; everything must be an object. Because of these limitations, Java is often much longer-winded than PHP, but is usually more reliable as it's much stricter - you can't get away with things that you could in PHP. Both Java and C++ are 'strongly typed', which means that if a variable can only contain one kind of thing once it's defined, you can't stick a string in it - e.g. this PHP would be illegal:

$a = 1;
$a = "hello";

Java has some practical differences that Neester mentioned, but they're not necessarily anything to do with OO. Java servlets (JSP) typically runs using an application server which continues running between page requests - this is partly because Java normally has quite a big start-up penalty, whereas PHP starts from scratch with every hit (though you can improve on things using sessions and serialization). It also means you need to code more carefully in Java than PHP as scripts stay running for much longer, so a memory leak that would cause no problem in PHP could be fatal to a Java app.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now