This example PHP program demonstrates how to list and access the class and object members of a user-defined type.
<?php class CMass { // Class Members static $zsaLiturgicalSeasons = ['Advent', 'Christmas', 'Lent', 'Paschal Triduum', 'Easter', 'Ordinary Time']; static $zsaLiturgicalColors = ['White', 'Red', 'Green', 'Violet', 'Black', 'Rose', 'Gold']; private static $zsCurrentSeason = 'Christmas'; public static $zsCurrentColor = 'White'; public static function IsALiturgicalColor($sTestColor) { foreach (self::$zsaLiturgicalColors as $sColor) { if ($sColor == $sTestColor) { return true; } } return false; } public static function GetCurrentSeason() { return self::$zsCurrentSeason; } private static function GetCurrentColor() { return self::$zsCurrentColor; } // Object Members private $msLiturgicalSeason; var $msLiturgicalColor; public function __construct($sSeason, $sColor) { $this->msLiturgicalSeason = $sSeason; $this->msLiturgicalColor = $sColor; } public function IsAPenitentialTime() { return ($this->GetColor() == 'Violet'); } public function GetSeason() { return $this->msLiturgicalSeason; } private function GetColor() { return $this->msLiturgicalColor; } public function GetMethods() { return get_class_methods($this); } public function GetVars() { return get_object_vars($this); } } echo "<b>Get Class Methods (excludes private methods):</b><br />"; $saClassMethods = get_class_methods('CMass'); foreach ($saClassMethods as $sKey => $sValue) { echo "$saClassMethods[$sKey] = $sValue<br />"; } echo "<br />"; echo "<b>Get Class Methods (includes private methods):</b><br />"; $qMass = new CMass('Lent', 'Violet'); $saClassMethods = $qMass->GetMethods(); foreach ($saClassMethods as $sKey => $sValue) { echo "$saClassMethods[$sKey] = $sValue<br />"; } echo "<br />"; echo "<b>Get Class Variables (excludes private variables):</b><br />"; $saClassVars = get_class_vars('CMass'); foreach ($saClassVars as $sKey => $sValue) { echo "$saClassVars[$sKey] = $sValue<br />"; } echo "<br />"; echo "<b>Get Object Variables (excludes private variables):</b><br />"; $qMass = new CMass('Lent', 'Violet'); $saObjectVars = get_object_vars($qMass); foreach ($saObjectVars as $sKey => $sValue) { echo "$saObjectVars[$sKey] = $sValue<br />"; } echo "<br />"; echo "<b>Get Object Variables (includes private variables):</b><br />"; $qMass = new CMass('Lent', 'Violet'); $saObjectVars = $qMass->GetVars(); foreach ($saObjectVars as $sKey => $sValue) { echo "$saObjectVars[$sKey] = $sValue<br />"; } echo "<br />"; echo "<b>Calling class methods:</b><br />"; echo "The current season is ".CMass::GetCurrentSeason()."<br />"; echo "CMass::GetCurrentColor() is not accessible.<br />"; $saTestColors = ['Red', 'Blue', 'Green', 'White', 'Yellow', 'Pink']; foreach ($saTestColors as $sTest) { $sStatement = (CMass::IsALiturgicalColor($sTest) ? "is" : "is not"); echo "$sTest ".$sStatement." a liturgical color.<br />"; } echo "<br />"; echo "<b>Calling object methods:</b><br />"; $qMass = new CMass('Lent', 'Violet'); $sStatement = ($qMass->IsAPenitentialTime() ? "is" : "is not"); echo "It ".$sStatement." a penitential time.<br />"; echo "The liturgical season is ".$qMass->GetSeason().".<br />"; echo "$qMass->GetColor() is not accessible.<br />"; ?>
© 20072024 XoaX.net LLC. All rights reserved.