php) 클래스, 함수, 생성자, array를 활용한 CRUD

<h1>
    <?php
        class User {
            public $uid, $pwd, $name, $email, $phone;

            public function __construct($uid, $pwd, $name, $email, $phone){
                $this->uid = $uid;
                $this->pwd = $pwd;
                $this->name = $name;
                $this->email = $email;
                $this->phone = $phone;
            }
            public function __toString() {
                return "{$this->uid} | {$this->pwd} | {$this->name} | {$this->email} | {$this->phone}";
            }
         }


    // CRUD
    // C
    global $arr;
    $arr = array();
    $lucas = new User("lucas",52,"SungMin","lucas@gmail.com","213.909.1687");
    $yona = new User("yona",21,"HeeWon","yona_@gmail.com","213.909.1687");
    $huhu = new User("huhu",38,"YungHu","huhu@gmail.com","555.555.5555");
    $shine = new User("shine",30,"SeungHyun","shine@gmail.com","333.333.3333");
    $arr[0] = $lucas;
    array_push($arr,$yona);
    array_push($arr,$huhu);
    array_push($arr,$shine);
    // R
    function read($arr){
        echo "Read"."<br>";
        foreach ($arr as $item){
            echo $item."<br>";
        }
    }

        read($arr);

    // U
    $newLucas = new User("lucas",5227,"SungMin","lucas@gmail.com","213-909-1687");
    for ($i=0;$i<sizeof($arr);$i++){
        if ($arr[$i]->uid==$newLucas->uid)
            $arr[0]=$newLucas;
    }
    echo "Update Sucess"."<br>";
    read($arr);

    // D
    unset($arr[2]);
    unset($arr[3]);
    echo "Delete Sucess"."<br>";
    read($arr);
    ?>
</h1>

'php' 카테고리의 다른 글

php) Cookie / Session  (0) 2022.02.28
php) File IO 업로드/다운로드  (0) 2022.02.27
php) 배열  (0) 2022.02.26
php) 연산자, 반복문  (0) 2022.02.25
php) 주석,숫자,문자,변수,상수  (0) 2022.02.24
Comment