C ++ Vs PHP - Object Oriented Issues:

I have been working in PHP recently, and while I find that the language is quite simple, coming from C ++ / C # / python, etc., I came across some strange differences (maybe) when it comes to its representations OO. If anyone could answer a few short questions, I would be very grateful :)

  • Can a constructor return the result value in PHP?

  • When a member function inside a class calls another member function inside a class, do I need to use self :: scoping or is this just a hint?

  • Why is there self :: and $ this-> and what's the difference?

  • Is there a need to delete an object created using a new one, or leave the sphere to delete it? I'm not sure if this is really dynamic, or if garbage collection is like in C #.

I know the questions are a little simple and I continue to see code that uses all these things - but I haven’t seen anything specific and I don’t have a good php book at home :) So thanks in advance for the answers!

+4
source share
2 answers
  • No, it automatically returns an instance of $this (if no exception is thrown)
  • Using self:: is required when accessing static members
  • self:: intended to access static members, $this-> is, for example, members
  • No, the object will be garbage collected when all references to it disappear
+5
source

1. Can the constructor return the value of the result in PHP?

No. (It was possible, but the problem has been fixed - if you see code that offers something else.)

2. When a member function inside the class calls another member function inside the class, do I need to use self :: scoping or is it just a hint?

This usually works technically, please do not do this. Inside object instances, $this to access its own properties and methods.

3. Why is there self :: and $ this-> and what's the difference?

This is not a complete answer, but for introduction: self:: is for static function calls and member access. See PHP: self vs. $ this .

4. Do I need to delete the object created using the new one or exit it, delete it? I'm not sure if it is really dynamic, or if garbage collection, like in C #.

You do not need to delete objects, there is a garbage collector. When objects leave the scope, they are deleted (the reference count to zval is one). Keep in mind that everything is deleted at the end of the request in PHP. Usually your application only works for a split second, and then the process memory is cleared just like script (and PHP) terminates.

+7
source

All Articles