Thursday, April 20, 2017

Class Example

<?php
class Article {
 public $name;
 public $main_category;
 public $tags = array();
 public $is_published = false;
 
 function __construct( $args = array(), $tags = array() ) {
  $this->tags = $tags;
  foreach ($args as $varname => $value) {
   echo "assign " . $varname . " to " . $value . "<br/>";
   $this->$varname = $value;
  }
 } 
 function category() {
  return $this->main_category;
 }
}
$article = new Article(array('name' => "Php Global Variable", 'main_category' => "Php Variables", 'is_published' => true), array("php"));

echo "Name: " . $article->{'name'} . ", Category: " . $article->{'category'}();

?>
Output:
assign name to Php Global Variable
assign main_category to Php Variables
assign is_published to 1
Name: Php Global Variable, Category: Php Variables

No comments:

Post a Comment

here docs

$x = <<<THE_END PHP stands for "PHP: Hypertext Preprocessor". The acronym "PHP" is therefore, usually referre...