Loading...

oops in php

Class best practices

  1. Capitalize and camelcase class names
  2. Make nouns singular
  3. Define each class in a seprate file
  4. group class files in a directory

Functions for Classes:

  1. get_declared_classes():-Returns an array with the name of the defined classes
  2. class_exists($string):-Checks if the class has been defined

File:class_example.php 

<?php
class Student{

}
$classes=get_declared_classes();
echo "classes:".implode(',',$classes).'<br>';

$class_names=['Teacher','Student','student'];

foreach($class_names as $class_name){

  if(class_exists($class_name))
  {
   echo "{$class_name} is declared class.<br>";
  }
  else
  {
    echo "{$class_name} is not declared class.<br>";
  }
}

?>

 Output

Teacher is not a declared class
Student is a declared class
student is a declared class