{"id":93,"date":"2008-07-27T21:28:25","date_gmt":"2008-07-28T01:28:25","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=93"},"modified":"2008-07-27T21:28:25","modified_gmt":"2008-07-28T01:28:25","slug":"php-class","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=93","title":{"rendered":"PHP Class"},"content":{"rendered":"<p><strong>Constructor<\/strong><\/p>\n<pre lang=\"php\">\r\n<?php\r\n  class Bird {\r\n    \/\/ Constructor\r\n    function __construct($name, $breed, $price){\r\n      $this->name = $name;\r\n      $this->breed = $breed;\r\n      $this->price = $price;\r\n    }\r\n    \r\n    function testAll(){\r\n      $this->testConstructor();\r\n    }\r\n    \r\n    function testConstructor(){\r\n      \/\/ Usage\r\n      $tweedy = new Bird('Tweedy','canary',15);\r\n      echo $tweedy->name;\r\n    }\r\n  }\r\n  $bird = new Bird();\r\n  $bird->testAll();\r\n?>\r\n<\/pre>\n<p><strong>Constructor with Default Values<\/strong><\/p>\n<pre lang=\"php\">\r\n<?php\r\n  class Bird {\r\n    \/\/ Constructor with default values\r\n    function __construct($name='No Name', $breed='Unknown', $price=15){\r\n      $this->name = $name;\r\n      $this->breed = $breed;\r\n      $this->price = $price;\r\n    }\r\n    \r\n    function testAll(){\r\n      $this->testConstructor();\r\n    }\r\n    \r\n    function testConstructor(){\r\n      \/\/ Usage\r\n      $unknown = new Bird();\r\n      echo $unknown->name;\r\n    }\r\n  }\r\n  $bird = new Bird();\r\n  $bird->testAll();\r\n?>\r\n<\/pre>\n<p><strong>Destructor<\/strong><\/p>\n<pre lang=\"php\">\r\n\/\/-------- Destructor\r\npublic function __descruct(){\r\n  $this->saveInfo();\r\n}\r\n<\/pre>\n<p>Magic Methods<\/p>\n<pre lang=\"php\">\r\n__construct() \r\nwhen new object is created\r\n__destruct()\r\nWhen object is destroyed\r\ne.g. unset($instance) or end of script\r\n__destroy() \r\n__autoload()\r\nRefer to the class for the first time\r\nCall constructor\r\nCall static methods\r\n__clone()\r\n__get() and __set()\r\nWhen property being get or set is not found \r\n__call()\r\nWhen method being invoked is not found\r\n__sleep()\r\nWhen serialized\r\n__wakeup()\r\nWhen unserialized\r\n__toString()\r\n<\/pre>\n<p><strong>Exception Handling<\/strong><\/p>\n<pre lang=\"php\">\r\ntry\r\n{\r\n  perform_some_action();\r\n  if($some_action_results_in_error)\r\n    throw new Exception(\"Houston, we've got a problem...\");\r\n\r\n  perform_another_action();\r\n  if($other_action_results_in_error)\r\n    throw new Exception(\"Houston, we've got a different problem...\");\r\n}\r\ncatch Exception $e\r\n{\r\n  handle_exception($e);\r\n}\r\n<\/pre>\n<p><strong>Member Visibilities<\/strong><\/p>\n<pre lang=\"php\">\r\nPublic (default)\r\nprivate\r\nprotected\r\n\r\nclass Bird {\r\n  \/\/------- Variables\r\n  private $name;\r\n  private $breed;\r\n  private $price;\r\n}\r\n<\/pre>\n<p>Static Member and self Keyword<\/p>\n<pre lang=\"php\">\r\n<?php \r\n  class ODebug {\r\n    \/\/------- Helper methods\r\n    public static function debug($msg, $indent=0){\r\n      $indentStr = \"\";\r\n      for ($i = 0; $i < $indent; $i++){\r\n        $indentStr = $indentStr.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';\r\n      }\r\n      echo $indentStr.$msg.'<br\/>';\r\n    }\r\n    \r\n    public function testDebug(){\r\n      \/\/ self means current class; no $ and always ::\r\n      self::debug('Test debug message');\r\n    }\r\n  }\r\n  \r\n  $odebug = new ODebug();\r\n  $odebug->testDebug();\r\n  ODebug::debug('Test debug',2);\r\n?>\r\n<\/pre>\n<p><strong>Constants<\/strong><\/p>\n<pre lang=\"php\">\r\n<?php \r\n  class ODebug {\r\n    \/\/ Define a constant. \r\n    \/\/ No access modifier allowed. Default to public static\r\n    const INDENT_CHAR='&nbsp;&nbsp;&nbsp;';\r\n    \r\n    \/\/------- Helper methods\r\n    public static function debug($msg, $indent=0){\r\n      $indentStr = \"\";\r\n      for ($i = 0; $i < $indent; $i++){\r\n        $indentStr = $indentStr.self::INDENT_CHAR;\r\n      }\r\n      echo $indentStr.$msg.'<br\/>';\r\n    }\r\n    \r\n    public function testDebug(){\r\n      self::debug('Test debug message');\r\n    }\r\n  }\r\n  \r\n  $odebug = new ODebug();\r\n  $odebug->testDebug();\r\n  ODebug::debug('Test debug',2);\r\n?>\r\n<\/pre>\n<p><strong>Class Inheritance:<\/strong><br \/>\nJava like<br \/>\n-Single inheritance<br \/>\n-Multiple interfaces<\/p>\n<pre lang=\"php\">\r\n<?php\r\n  require_once('Bird.php');\r\n  require_once('ODebug.php');\r\n  class Tweedy extends Bird{\r\n    public function chirp(){\r\n      ODebug::debug(\"Tweedy tweedy...\");\r\n    }\r\n  }\r\n  \r\n  class Parrot extends Bird{\r\n    public function __construct($name){\r\n      parent::__construct($name,\"Parrot\",50);\r\n    }\r\n    \r\n    public function chirp(){\r\n      ODebug::debug(\"Qwark Qwark...\");\r\n    }\r\n  }\r\n  $tweedy = new Tweedy('Tweedy2','Still Canary',30);\r\n  $tweedy->chirp();\r\n  $tweedy->display();\r\n\r\n  $parrot = new Parrot(\"Patty\");\r\n  $parrot->chirp();\r\n  $parrot->display();\r\n?>\r\n<\/pre>\n<p><strong>Abstract and Final Modifier<\/strong><br \/>\nabstract public function birdCall();<br \/>\nfinal public function birdCall();<br \/>\n<strong>Interfaces<\/strong><br \/>\n-Can only declare public functions<br \/>\n-Can not declare variables<\/p>\n<pre lang=\"php\">\r\n<?php \r\n  interface IPet {\r\n    public function getName();\r\n    public function getBreed();\r\n  }\r\n\r\nrequire_once('IPet.php');\r\nclass Bird implements IPet{\r\n    public function getName(){\r\n        return $this->name;\r\n    }\r\n    public function getBreed(){\r\n        return $this->breed;\r\n    }\r\n?>\r\n<\/pre>\n<p><strong>Reflection API<\/strong><\/p>\n<pre lang=\"php\">\r\nclass_exists($class,$autoload=true)\r\nClass_implements($class)\r\nClass_parents($class)\r\nGet_class_methods($class)\r\nGet_class_vars($class)\r\nget_class($instance)\r\nGet_declared_classes()\r\nGet_declared_interfaces()\r\nGet_object_vars($instance)\r\nGet_parent_class($class)\r\nInterface_exists($interface,$autoload=true)\r\nIs_a($instance, $class)\r\nMethod_exists($instance, $method)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Constructor Constructor with Default Values Destructor \/\/&#8212;&#8212;&#8211; Destructor public function __descruct(){ $this->saveInfo(); } Magic Methods __construct() when new object is created __destruct() When object is destroyed e.g. unset($instance) or end of script __destroy() __autoload() Refer to the class for the &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=93\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[20],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-1v","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=93"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/93\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}