skule@sormo.no
  • Start
  • Programmering
    • yy
    • Archived Articles
    • Create Article
    • List All Categories in an Article Category Tree
    • Category Blog
    • Category List
    • Bilagsregistrering
  • Teknikk
  • Media
  • Undervisning
  • Nettbutikk
  • Logg inn
  • Person
  1. Du er her:  
  2. Hjem
  3. Programmering
  4. List All Categories in an Article Category Tree
  5. Artikler

PHP objekter og classes. Eksempel

Detaljer
Treff: 5

<?php
    class Person {
        // first name of person
        private $name;
        
        // public function to set value for name (setter method)
        public function setName($name) {
            $this->name = $name;
        }
        
        // public function to get value of name (getter method)
        public function getName() {
            return $this->name;
        }
    }

    // creating class object
    $john = new Person();
    
    // calling the public function to set fname
    $john->setName("John Wick");
    
    // getting the value of the name variable
    echo "My name is " . $john->getName();

 

 

    class Person1 {
        // first name of person
        public static $name;
        
        // public function to get value of name (getter method)
        public static function getName() {
            return self::$name;     // using self here
        }
    }
$r=new Person1();
$r::$name='Donald';
echo'<br>person1  '.Person1::getName().'<br>';

 

    class Job {
        // opening for position
        public $name;
        // description for the job;
        public $desc;
        // company name - as the company name stays the same
        public static $company;
        
        // public function to get job name
        public function getName() {
            return $this->name;
        }
        
        // public function to get job description
        public function getDesc() {
            return $this->desc;
        }
        
        // static function to get the company name
        public static function getCompany() {
            return self::$company;
        }
        
        // non-static function to get the company name
        public static  function getCompany_nonStatic() {
            return self::getCompany();
        }
    }
    
    $objJob = new Job();
    // setting values to non-static variables
    $objJob->name = "Data Scientist";
    $objJob->desc = "You must know Data Science";
    
    /* 
        setting value for static variable.
        done using the class name
    */
    Job::$company = "Studytonight";
    
    // calling the methods
    echo "Job Name: " .$objJob->getName()."<br/>";
    echo "Job Description: " .$objJob->getDesc()."<br/>";
    echo "Company Name: " .Job::getCompany_nonStatic();


$a=new Person();
$a->setName("per");
echo $a->getName();
//echo '<br>';
$b=new Person();
$b->setName("ola");
echo $b->getName();

$c=new Person1();

 

?>

Side 3 av 3

  • 1
  • 2
  • 3

logg inn

  • Glemt passord?
  • Glemt brukernavn?