Q: What is Class constants ?
A constant is, just like the name
implies, a variable that can never be changed. When you declare a constant, you
assign a value to it, and after that, the value will never change. Normally,
simple variables are just easier to use, but in certain cases constants are
preferable, for instance to signal to other programmers (or your self, in case
you forget) that this specific value should not be changed during runtime.
Class constants are just like regular
constants, except for the fact that they are declared on a class and therefore
also accessed through this specific class. Just like with static members, you
use the double-colon (Scope resolution) operator to access a class constant.
Here is a basic example
class user
{
const DefaultUsername
= "RAJARAM";
const MinimumPasswordLength
= 6;
}
echo "The default username is " . user::DefaultUsername;
echo "The minimum password length is " . user::MinimumPasswordLength;
?>
As you can see, it's much like
declaring variables, except there is no access modifier - a constant is always
publically available. As required, we immediately assign a value to the
constants, which will then stay the same all through execution of the script.
To use the constant, we write the name of the class, followed by the
double-colon operator and then the name of the constant.
Q: Explain The
"final" keyword
In some cases you may want to prevent
a class from being inherited from or a function to be overridden. This
can be done with the final keyword, which simply causes PHP to throw an error
if anyone tries to extend your final class or override your final function.
A final class could look like this:
final class Animal
{
public $name;
}
A class with a
final function could look like this:
class Animal
{
final public function Greet()
{
return "The final word!";
}
}
The two can be combined if you need to,
but they can also be used independently, as seen in the examples above.
The spl_autoload_register() function
registers any number of autoloaders, enabling for classes and interfaces to be
automatically loaded if they are currently not defined. By registering
autoloaders, PHP is given a last chance to load the class or interface before
it fails with an error.
If one class depends on another, if
your application ever changes, you have to work extra hard to be sure that the
relationships between your classes are maintainable. But with autoloading
classes in PHP this may not be necessary.
·
__autoload( ) —
Attempt to load undefined class , void __autoload ( string $class )
·
spl_autoload_register()
- Register given function as __autoload() implementation
Example #1 Autoload example
This example attempts to load the
classes MyClass1 and MyClass2 from the files MyClass1.php and MyClass2.php
respectively.
spl_autoload_register(function ($class_name) {
include $class_name . '.php';
});
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
include $class_name . '.php';
});
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
Example #2 Autoloading with exception
handling
This example throws an exception and
demonstrates the try/catch block.
spl_autoload_register(function ($name) {
echo "Want to load $name.\n";
throw new Exception("Unable to load $name.");
});
try {
$obj = new NonLoadableClass();
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
?>
echo "Want to load $name.\n";
throw new Exception("Unable to load $name.");
});
try {
$obj = new NonLoadableClass();
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
?>
Example #3
Image.php
class Image {
function __construct() {
echo 'Class Image loaded successfully
';
';
}
}
?>
Test.php
class Test {
function __construct() {
echo 'Class Test working
';
';
}
}
?>
Myprg.php
function
__autoload($class_name) {
if(file_exists($class_name
. '.php')) {
require_once($class_name
. '.php');
}
else {
throw
new Exception("Unable to load $class_name.");
}
}
try
{
$a
= new Test();
$b
= new Image();
}
catch (Exception $e) {
echo
$e->getMessage(), "\n";
}
?>
9 comments:
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
rpa training in chennai | rpa training in chennai
rpa training in pune | rpa online training | rpa training in bangalore
This is such a great post, and was thinking much the same myself. Another great update.
Data Science course in Chennai | Data science course in bangalore
Data science course in pune | Data science online course
Python course in Kalyan nagar
Awesome! Education is the extreme motivation that open the new doors of data and material. So we always need to study around the things and the new part of educations with that we are not mindful.
java training in chennai
java training in marathahalli | java training in btm layout
Great content thanks for sharing this informative blog which provided me technical information keep posting.
python training in tambaram | python training in annanagar | python training in jayanagar
Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
Best Devops Training in pune
Devops interview questions and answers
A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
rpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune
You really touched a few great points of interest and I genuinely admire you for that. Undoubtedly this might be advantageous for many seekers. Continue sharing and keep updating.
Website Designer in Lucknow | Web design company
Thank you for your post.
Dot Net Classes in Chennai | Dot Net Training Center in Chennai
Selenium Testing Training in Chennai | Selenium Training Institute in Chennai
Best Software Testing Training Institute in Chennai | Testing Courses in Chennai
Java Training Institute in Chennai | Core Java Training in Chennai | Java Course and Certification
PHP Course in Chennai | PHP Training Institute in Chennai | PHP Course and Certification
Post a Comment