PHP Interview Question and Answers

51. What is Constructors and Destructors?
  CONSTRUCTOR : PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

DESTRUCTORS : PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.
 
Your Name Your Email-ID
Your Answer
52. Why do we create an instance of a class?
  To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation (and in some cases this is a requirement).

If a string containing the name of a class is used with new, a new instance of that class will be created. If the class is in a namespace, its fully qualified name must be used when doing this.
 
Your Name Your Email-ID
Your Answer
53. What is properties of class?
  Class member variables are called "properties". We may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
 
Your Name Your Email-ID
Your Answer
54. Explain Constant in Class?
  It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that we don't use the $ symbol to declare or use them.

The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
 
Your Name Your Email-ID
Your Answer
55. Explain the visibility of the property or method?
  The visibility of a property or method must be defined by prefixing the declaration with the keywords public, protected or private.
  • Class members declared public can be accessed everywhere.
  • Members declared protected can be accessed only within the class itself and by inherited and parent classes.
  • Members declared as private may only be accessed by the class that defines the member.
 
Your Name Your Email-ID
Your Answer
123456789101112


131415161718 Page 12 of 18