PHP 객체 지향의 일부 클래스에 대한 코드 예제 요약
Jun 30, 2017 am 09:51 AM
php
암호
예
了解类
class_exists验证类是否存在
<?php // TaskRunner.php $classname = "Task"; $path = "tasks/{$classname}.php"; if ( ! file_exists( $path ) ) { throw new Exception( "No such file as {$path}" ); //抛出异常,类文件不存在 } require_once( $path ); $qclassname = "tasks\\$classname"; if ( ! class_exists( $qclassname ) ) { throw new Exception( "No such class as $qclassname" ); //抛出异常,类不存在Fatal error: Uncaught exception 'Exception' with message 'No such class as tasks\Task' Stack trace: #0 {main} } $myObj = new $qclassname(); $myObj->doSpeak(); ?>
로그인 후 복사
get_class 检查对象的类 instanceof 验证对象是否属于某个类
<?php class CdProduct {} function getProduct() { return new CdProduct( "Exile on Coldharbour Lane", "The", "Alabama 3", 10.99, 60.33 ); // 返回一个类对象 } $product = getProduct(); if ( get_class( $product ) == 'CdProduct' ) { print "\$product is a CdProduct object\n"; } ?> <?php class CdProduct {} function getProduct() { return new CdProduct( "Exile on Coldharbour Lane", "The", "Alabama 3", 10.99, 60.33 ); } $product = getProduct(); if ( $product instanceof CdProduct ) { print "\$product is a CdProduct object\n"; } ?>
로그인 후 복사
get_class_methods 得到类中所有的方法列表,只获取public的方法,protected,private的方法获取不到。默认的就是public。
<?php class CdProduct { function construct() { } function getPlayLength() { } function getSummaryLine() { } function getProducerFirstName() { } function getProducerMainName() { } function setDiscount() { } function getDiscount() { } function getTitle() { } function getPrice() { } function getProducer() { } } print_r( get_class_methods( 'CdProduct' ) ); ?>
로그인 후 복사
output:
Array ( [0] => construct [1] => getPlayLength [2] => getSummaryLine [3] => getProducerFirstName [4] => getProducerMainName [5] => setDiscount [6] => getDiscount [7] => getTitle [8] => getPrice [9] => getProducer )
로그인 후 복사
更多验证
<?php class ShopProduct {} interface incidental {}; class CdProduct extends ShopProduct implements incidental { public $coverUrl; function construct() { } function getPlayLength() { } function getSummaryLine() { } function getProducerFirstName() { } function getProducerMainName() { } function setDiscount() { } function getDiscount() { } function getTitle() { return "title\n"; } function getPrice() { } function getProducer() { } } function getProduct() { return new CdProduct(); } $product = getProduct(); // acquire an object $method = "getTitle"; // define a method name print $product->$method(); // invoke the method if ( in_array( $method, get_class_methods( $product ) ) ) { print $product->$method(); // invoke the method } if ( is_callable( array( $product, $method) ) ) { print $product->$method(); // invoke the method } if ( method_exists( $product, $method ) ) { print $product->$method(); // invoke the method } print_r( get_class_vars( 'CdProduct' ) ); if ( is_subclass_of( $product, 'ShopProduct' ) ) { print "CdProduct is a subclass of ShopProduct\n"; } if ( is_subclass_of( $product, 'incidental' ) ) { print "CdProduct is a subclass of incidental\n"; } if ( in_array( 'incidental', class_implements( $product )) ) { print "CdProduct is an interface of incidental\n"; } ?>
로그인 후 복사
output:
title title title title Array ( [coverUrl] => ) CdProduct is a subclass of ShopProduct CdProduct is a subclass of incidental CdProduct is an interface of incidental
로그인 후 복사
call方法
<?php class OtherShop { function thing() { print "thing\n"; } function andAnotherthing() { print "another thing\n"; } } class Delegator { private $thirdpartyShop; function construct() { $this->thirdpartyShop = new OtherShop(); } function call( $method, $args ) { // 当调用未命名方法时执行call方法 if ( method_exists( $this->thirdpartyShop, $method ) ) { return $this->thirdpartyShop->$method( ); } } } $d = new Delegator(); $d->thing(); ?>
로그인 후 복사
output:
thing
传参使用
<?php class OtherShop { function thing() { print "thing\n"; } function andAnotherthing( $a, $b ) { print "another thing ($a, $b)\n"; } } class Delegator { private $thirdpartyShop; function construct() { $this->thirdpartyShop = new OtherShop(); } function call( $method, $args ) { if ( method_exists( $this->thirdpartyShop, $method ) ) { return call_user_func_array( array( $this->thirdpartyShop, $method ), $args ); } } } $d = new Delegator(); $d->andAnotherThing( "hi", "hello" ); ?>
로그인 후 복사
output:
another thing (hi, hello)
위 내용은 PHP 객체 지향의 일부 클래스에 대한 코드 예제 요약의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

인기 기사
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD

인기 기사
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7297
9


자바 튜토리얼
1622
14


Cakephp 튜토리얼
1342
46


라라벨 튜토리얼
1259
25


PHP 튜토리얼
1206
29



Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법
