Wednesday, July 11, 2018

constants , final and autoload functions [BCA Sem-3]


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.

Q:EXPLAIN Autoloading Classes
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();
?>
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";
}
?>
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";
}
?>