PHP

[PHP] CLASS

98kg 2024. 2. 27. 22:31

 

PHP 도 oop 언어 중 하나로 객체지향으로의 성질을 가지고있다.

 

PHP에서의 Class 는 Java 와 일부분 비슷한 거 같다

클래스 선언, getter/setter 선언 그리고 setter/ getter 호출까지 알아보겠습니다.

 

<?php

class Fruit {
    // properties 
    public $name;
    public $color;


    function setName($name){
        $this -> name = $name;
    }
    function getName(){
        return $this -> name;
    }
  
    function setColor($color){
        $this -> color = $color;
    }
  
    function getColor(){
        return $this->color;
    }

    
  
}

$apple = new Fruit();
$apple -> setName('사과');
echo $apple->getName();