Server/PHP

Global 과 Static, Call by value, Call by reference

kirhieyes 2013. 1. 23. 00:42

1. Global

  - 함수 밖의 변수를 사용해야하는 경우 해당 변수를 global 로 선언한다.

<?PHP

    function counts(){

        global $i;

        $i = $i + 1;

    }

    $i = 0;

    while($i < 10){

        counts();

        print $i."<br>";

    }

?>

 

2. Static

  - 함수내의 변수는 호출할 때마다 초기화 된다. 그걸 막고 싶을 경우 static으로 선언한다.

<?PHP

    function inc(){

        static $i = 1;

        print $i."<br>";

        $i = $i + 1;

    }

    for($j = 1; $j <= 10; $j++)

        inc();

?>

// 1 ~ 10 까지 출력되는데 $i 가 static 선언이 안되어 있으면 계속 1로 초기화 되어서 1만 10번 찍힌다.

 

3. call by value

<?PHP

    function swap($a, $b){

        $temp = $a;

        $a = $b;

        $b = $temp;

    }

    $i = 3;

    $j = 4;

    swap($i, $j);

    print "$i, $j";    // 3, 4 가 출력됨, swap 에서 값만 넘기고 받지 않았으므로 i, j 는 동일함

?>

  - 위의 swap 은 실제 기능을 못하고 있다. 이를 고치면 다음과 같다.

<?PHP

    function swap($a, $b){

        global $$a, $$b;

        $temp = $$a;

        $$a = $$b;

        $$b = $temp;

    }

    $i = 3;

    $j = 4;

    swap('i', 'j');

    print "$i, $j"; // 4, 3 이 출력됨, 변수문자만 넘기고 swap에서 global 변수로 재생성함.

?>

4. call by reference

  - 메모리를 가리킬 때에는 변수에 '&' 표기를 한다.

<?PHP

    function swap(&$a, &$b){

        $temp = $a;

        $a = $b;

        $b = $temp;

    }

    $i = 3;

    $j = 4;

    swap($i, $j);

    print "$i, $j"; // 4, 3 이 출력됨

?>

 

5. file

 - open 함수 : int fopen(string filename, string mode)

 - mode

      - 'r' : 읽기 전용, 파일 포인터는 파일 시작에 위치

      - 'r+' : 읽고 쓰기, 포인터는 동일

      - 'w' : 쓰기 전용, 포인터는 파일 시작에 위치, 파일이 존재하면 내용을 삭제, 존재하지 않으면 파일 생성

      - 'w+' : 읽고 쓰기, 나머지 동일

      - 'a' : 쓰기 전용, 포인터는 파일의 끝, 파일이 존재하지 않으면 파일을 생성

      - 'a+' : 읽고 쓰기, 나머지 동일

 - close 함수 : bool fclose(int filepointer), 파일작업을 할때 해당 파일이 lock 걸리므로 open 이후에는 close를 해줘야 다른 사람이 쓸 수 있다.

 - write 함수 : int fputs(int filepointer, string str)

 - read 함수 : string fgets(int filepointer, int length)

 

<?PHP
 $filep = fopen("./exam.txt", "w");
 if(!$filep) die("file not open");
 fputs($filep, "1 2 3 4 5 6 7 8 9 0\n");
 fclose($filep);
 $filep = fopen("./exam.txt", "r");
 
 while ($line = fgets($filep, 1024))
  print $line."<br>";

 fclose($filep);
?>

 

6. 클래스

<?php
 class Student{
  // public 속성
  // php 는 private 이 없다.

  // 일반적으로 private 를 알려줄때 '_변수'로 쓴다.
  var $StudentID;
  var $StudentName;
  
  // 생성자
  function Student($id, $name){
   $this->StudentID = $id;
   $this->StudentName = $name;
  }
  // method
  function printStudent(){
   print "ID : ". $this->StudentID . "<br>";
   print "Name : " . $this->StudentName . "<br>";
  } 
 }
 $object = new Student(1234, "Kim");
 
 $object->printStudent();
 print "<br>";
 
 class People{
  var $Name;
  var $Age;
  
  function printPeople(){
   print "Name : " . $this->Name . "<br>";
   print "Age : " . $this->Age . "<br>";
  }
 } 
 // 상속
 class Professor extends People{
  var $Office_No;
  
  function printProfessor(){
   $this->printPeople();
   print "Office_No : " . $this->Office_No . "<br>";
  }
  function printProfessor2(){
   print "Name : " . $this->Name . "<br>";
   print "Age : " . $this->Age . "<br>";
   print "Office_No : " . $this->Office_No . "<br>";
  }
  function professor($name, $age, $no){
   $this->Name = $name;
   $this->Age = $age;
   $this->Office_No = $no;
  }  
 }
 
 $object = new Professor("Kim", "37", "105");
 $object->printProfessor();
 print "<br>";
 
 $object->printProfessor2();
?>

 

7. 정규표현식

 - preg_match('/pattern/i', $string, $matches)

 

<?php
 $urls = array("www.samsung.co.kr", "www.daum.net", "www.donga.co.kr", "www.chosun.com", "www.kbs.co.kr", "vinus.donga.co.kr", "www.LIST.co.kr", "www.note.abc.co.kr");
 print "preg_match() case<br>";


 //preg_match('%pattern%i', $string, $matches)
 foreach($urls as $url){
  if(preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $url))
   print $url . "<br>";
 } 
?>

 

8.