Core PHP

A Class with Methods

When sorting associative arrays, we generally want to key the keys and values paired together and be able to sort based on either one. Key and value based sorting for associative arrays is demonstrated in the program below.

CPrayer.php

<?php
  class CPrayer {
    var $msTitle;
    var $msaText;

    public function __construct($sTitle, $sText) {
      $this->msTitle = $sTitle;
      $this->msaText = array();
      $sLine = strtok($sText, "\n");
      while ($sLine !== false) {
        $this->msaText[] = $sLine;
        $sLine = strtok("\n");
      }
    }

    public function GetTitle() {
      return $this->msTitle;
    }

    public function PrintAll() {
      foreach ($this->msaText as $sCurrLine) {
        echo $sCurrLine."<br />";
      }
      echo "<br />";
    }
  }

  $qGloria = new CPrayer("Gloria", "Glory to God in the highest,
  and on earth peace to people of good will.
  We praise you, we bless you, we adore you, we glorify you,
  we give you thanks for your great glory, Lord God, heavenly King,
  O God, almighty Father.
  Lord Jesus Christ, only begotten Son,
  Lord God, Lamb of God, Son of the Father,
  you take away the sins of the world, have mercy on us;
  you take away the sins of the world, receive our prayer;
  you are seated at the right hand of the Father, have mercy on us.
  For you alone are the Holy One,
  you alone are the Lord,
  you alone are the Most High, Jesus Christ,
  with the Holy Spirit,
  in the glory of God the Father.
  Amen.");
  echo "<h2>".$qGloria->GetTitle()."</h2>";
  $qGloria->PrintAll();


  $qOurFather = new CPrayer("Our Father", "Our Father,
  who art in heaven,
  hallowed be thy name;
  thy kingdom come,
  thy will be done,
  on earth as it is in heaven.
  Give us this day
  our daily bread
  and forgive us our trespasses,
  as we forgive those
  who trespass against us
  and lead us not into temptation,
  but deliver us from evil.
  Amen.");
  echo "<h2>".$qOurFather->GetTitle()."</h2>";
  $qOurFather->PrintAll();
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.