When you declare a class, you are really making a template for the creation of objects. You list all the variables the object should have and all the functions it will need. Sometimes these are called properties and methods, respectively. Note that inside the curly braces you can only declare variables with the var statement or declare functions.
Defining a class
When you declare a property, you don't specify a data type. It is a variable like any other, and it may contain an integer, a string, or even another object. Depending on the situation, it might be a good idea to add a comment near the declaration of the property that states its intended use and data type. When you declare a method, you do so just as you would a function outside a class definition. Both methods and properties exist within their own scope, or name space. That means you can safely create methods that have the same name as functions declared outside of class definitions without conflicts. An exception to this are built-in functions. For example, you cannot have a print method.
Aside from the variables passed as arguments, methods contain a special variable called this. It stands for the particular instance of the class. You must use this to refer to properties and other methods of the object. Some object-oriented languages assume an unqualified variable that refers to a local property, but in PHP any variables referred to within a method are simply variables local to that scope. Note the use of the this variable in the constructor for the user class.
If you choose to declare a function within a class that has the same name as the class itself, the function will be considered a constructor and will be executed immediately upon creating an object from that class. Typically the constructor is used to initialize the object's properties. Like any other function, the constructor may have parameters and even default values. You can set up classes that allow you to create an object and set all its properties in one statement. Unlike other languages, PHP does not allow for destructors—functions that execute when the instance is deleted. However, if you choose to use unset on an object, all the memory associated with that object will be freed. In situations where you must execute some code when you finish using an object, create your own shutdown function and remember to call it.
One powerful aspect of classes is inheritance, the idea that a class can extend the functionality of another class. The new class will contain all the methods and properties of the class it extends, plus any others it lists within its body. You may also override methods and properties from the extended class.
One issue you might wonder about is whether and how constructors are inherited. While they are inherited along with all other methods, they cease to have the property of being called when an object is created from the class. If you require this functionality, you must write it explicitly by calling the parent class's constructor within the child class's constructor.
PHP Related Interview Questions
|PHP Interview Questions||MySQL Interview Questions|
|PHP+MySQL Interview Questions||Drupal Interview Questions|
|MYSQL DBA Interview Questions||PHP5 Interview Questions|
|WordPress Interview Questions||Joomla Interview Questions|
|CakePHP Interview Questions||CodeIgniter Interview Questions|
|PHP7 Interview Questions|
An Introduction To Php
Variables, Operators, And Expressions
Classes And Objects
I/o And Disk Access
Time, Date, And Configuration Functions
Parsing And String Evaluation
Sorting Searching And Random Numbers
Integration With Html
Efficiency And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.