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";
}
?>
15 comments:
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
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
The Blog is very Impressive. every content should be unique and explained every concept should be Clear.
Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
Great article containing lot of sources.Thank you. Java training in Chennai | Certification | Online Course Training | Java training in Bangalore | Certification | Online Course Training | Java training in Hyderabad | Certification | Online Course Training | Java training in Coimbatore | Certification | Online Course Training | Java training in Online | Certification | Online Course Training
Great post and informative blog. It was awesome to read, thanks for sharing this great content to my vision.Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating
Data Science Training In Chennai
Data Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training
I came on this piece while looking for information on blog research... Keep posting and updating information, it's a wonderful post. A new article concerning space bar counter can be found here. You can use the spacebar clicker test to see how quickly you can push the spacebar. For further information, go to this blog.
Here all content so useful and helpful for beginner and experience both.This site is so amazing, This sites gives good knowledge of c-cpp-training ,This is very helpful for me.
Purgesoft is the best Custom Software Development Company. We have delivered our services for web development, mobile app development, and digital marketing services to several businesses. Delivering our services for decades
Get The Most Professional & Easy To Use Author Website Design With 100% Satisfaction & Peace of Mind. we provide Customized websites at an unbeatable price for authors since 2010.
Post a Comment