강의 컨설팅 트레이닝 무료진단 무료책자 마케팅편지 마케팅정보공유 다이어리 서비스제휴 고객센터

PHP도 객체지향형 프로그램이다..!!(클래스,상속동...)
작성자 : 13 김영철
등록날짜 : 2009.01.13 13:12
1,216
Java나 C++을 쓰시면서 클래스의 매력을 알고 있으나 
php는 없어 실망하신 분들... 
php는 대단합니다. 

기본지식 
---- 객체자신 호출은 $this로 합니다 
---- ex)$this -> name; 

About 
여기에 있는 함수들은 클래스와 객체에 대한 정보를 얻는데 도움을 줍니다. 여러분은 이 함수들을 사용하여 어떤 객체가 속한 클래스의 이름, 그리고 그 객체의 속성(멤버 함수(메소드,method)와 속성(멤버변수 등.))을 찾아낼수 있습니다. 여기있는 함수들을 사용하면 어떤 객체의 속성및 메소드뿐만 아니라, 그 객체의 혈통도 알아낼 수 있습니다.(즉, 그 객체가 어떤 클래스의 상속을 받았는지를 알아낼 수 있습니다.) 

사용방법 
여기에 있는 예제에서는 먼저 기본적인 형태의 클래스를 정의하고, 그 클래스의 상속을 받은 클래스를 정의합니다. 여기의 기본적인 형태의 클래스는 일반적인 야채에 대한 클래스로, 그 야채가 먹을수 있는 것인지 혹은 그렇지 않은지, 또한 그 야채의 색깔은 무엇인지를 정의합니다. 그 클래스의 하위클래스(자식 클래스)는 시금치 클래스로 요리를 할수 있는 멤버함수(메소드, method)와 "시금치 객체가 요리를 할수 있는지를 알아내는 다른 멤버함수들을 추가 합니다. 

예 1. classes.inc 

<?php 

// 멤버변수(속성)와 멤버함수를 가지는 기본 클래스 

    
class Vegetable 


    var 
$edible

    var 
$color


    function 
Vegetable$edible$color="green" 
) { 
        
$this->edible $edible

        
$this->color $color

    } 

    function 
is_edible
() { 
        return 
$this->edible

    } 

    function 
what_color
() { 
        return 
$this->color

    } 
     
// end of class Vegetable 


// 기본클랙스의 상속(확장) 
class Spinach extends Vegetable 


    var 
$cooked false


    function 
Spinach
() { 
        
$this->Vegetabletrue"green" 
); 
    } 

    function 
cook_it
() { 
        
$this->cooked true

    } 

    function 
is_cooked
() { 
        return 
$this->cooked

    } 
     
// 시금치 객체의 끝 

?> 
       




위의 클래스들로 부터 2개의 객체를 생성하면, 그 클래스의 혈통을 포함한 클래스들에 대한 정보를 알아낼수 있습니다. 변수들을 깔끔하게 출력하기 위해 몇개의 멤버함수들을 추가해서 정의할수 있습니다. 

예 2. test_script.php 

<pre> 
<?php 

include "classes.inc"


// utility functions 

function print_vars($obj
) { 
    
$arr get_object_vars($obj
); 
    while (list(
$prop$val) = each($arr
)) 
        echo 
"t$prop = $valn"



function 
print_methods($obj
) { 
    
$arr get_class_methods(get_class($obj
)); 
    foreach (
$arr as $method

        echo 
"tfunction $method()n"



function 
class_parentage($obj$class
) { 
    global $
$obj

    if (
is_subclass_of($$obj$class
)) { 
        echo 
"Object $obj belongs to class ".get_class($$obj
); 
        echo 
" a subclass of $classn"

    } else { 
        echo 
"Object $obj does not belong to a subclass of $classn"

    } 


// instantiate 2 objects 

$veggie = new Vegetable(true,"blue"
); 
$leafy = new Spinach
(); 

// print out information about objects 
echo "veggie: CLASS ".get_class($veggie)."n"

echo 
"leafy: CLASS ".get_class($leafy
); 
echo 
", PARENT ".get_parent_class($leafy)."n"


// show veggie properties 
echo "nveggie: Propertiesn"

print_vars($veggie
); 

// and leafy methods 
echo "nleafy: Methodsn"

print_methods($leafy
); 

echo 
"nParentage:n"

class_parentage("leafy""Spinach"
); 
class_parentage("leafy""Vegetable"
); 
?> 
</pre> 
       




위의 예제에서 중요한 것은, $leafy 객체는 Vegetable 클래스의 하위클래스인 시금치클래스의 객체라는 것입니다. 위의 예제는 다음과 같은 결과가 출력됩니다. 



       [...] 
Parentage: 
Object leafy does not belong to a subclass of Spinach 
Object leafy belongs to class spinach a subclass of Vegetable 
       




차례 
call_user_method &#8212; 주어진 객체의 사용자 메소드(멤버함수)를 호출합니다. 
class_exists &#8212; 클래스가 정의 되어있는지 검사합니다. 
get_class &#8212; 어떤(임의의) 객체가 속한 클래스의 이름을 반환합니다. 
get_class_methods &#8212; 클래스의 기본적인 속성(멤버변수)들을 배열로 반환합니다. 
get_class_vars &#8212; 클래스의 기본적인 속성(멤버변수)들을 배열로 반환합니다. 
get_declared_classes &#8212; Returns an array with the name of the defined classes 
get_object_vars &#8212; 객체의 속성(멤버변수)들을 associative 배열로 반환한다. 
get_parent_class &#8212; 객체의 상위(아버지) 클래스의 이름을 반환합니다. 
is_subclass_of &#8212; 인수로 주어진 클래스의 하위(자식)클래스의 객체인지 검사합니다. 
method_exists &#8212; 클래스의 멤버함수(메소드)가 존재하는지 검사합니다. 
User Contributed Notes: 클래스/객체 함수 
gateschris@yahoo.com 
08-Mar-2001 01:59 
[Editor's note: If you are trying to do overriding, then you can just interrogate (perhaps in the method itself) about what class (get_class()) the object belongs to, or if it is a subclass of a particular root class. 

You can alway refer to the parent overriden method, see the "Classes and Objects" page of the manual and comments/editor's notes therein.] 

There is no function to determine if a member belongs to a base class or current class eg: 

class foo { 
function foo () { } 
function a () { } 


class bar extends foo { 
function bar () { } 
function a () { } 


lala = new Bar(); 
------------------ 
how do we find programmatically if member a now belongs to class Bar or Foo. 

<<위 내용은 http://www.php.net/manual/kr/ref.classobj.php에서 
출처하여 수정했슴>>  

[출처] 웹디황용

"쇼핑몰·홈페이지·오픈마켓
블로그·페이스북·이메일 등의 각종 마케팅 글쓰기,
각종 광고, 영업, 판매, 제안서, 전단지
반응율 3배×10배 이상 높이는 마법의 8단계 공식"
자세히보기

Comments

마케팅
특별 마케팅자료
다운로드 마케팅자료
창업,경영
기획,카피,상품전략
동기부여,성취