PHP의 공개 함수

王林
풀어 주다: 2024-08-29 13:08:00
원래의
750명이 탐색했습니다.

다른 많은 객체 지향 프로그래밍 언어와 마찬가지로 PHP에도 프로그램 내에서 함수의 접근성을 나타내는 방법이 있습니다. 공개(Public), 보호(protected), 비공개(private)가 사용되는 키워드이며, 공개(public)는 특정 PHP 프로그램에서 전역적으로 함수에 액세스할 수 있음을 나타냅니다. 함수를 public으로 선언하면 많은 장점이 있는데, 그 중 하나는 함수를 프로그램 내 어디에서나 제한 없이 호출하고 사용할 수 있다는 것입니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

PHP에서 공개 함수는 어떻게 작동하나요?

공개 기능은 제한 없이 작동합니다. 공용 함수는 클래스 외부, PHP의 프로그래밍 코드 내 클래스 내부 및 기타 프로그래밍 언어에서도 작동합니다. 공용 함수는 해당 클래스의 전체 콘텐츠를 해당 클래스에 액세스할 때만 다른 클래스에서 사용할 수 있도록 합니다. 공개 함수임에도 불구하고 접근하지 않으면 아무 일도 하지 않습니다. PHP 퍼블릭 함수는 클래스 내의 다른 클래스에서/액세스하지 않고는 아무것도 작동/구현하지 않습니다.

PHP에서 공개 함수를 구현하는 예

다음은 공개 기능을 구현하는 몇 가지 예입니다.

예시 #1

공개 함수/수정자의 예입니다. 아래의 잘 설명된 예를 통해 어떻게 작동하는지 확인하세요.

코드:

<?php
// BaseClass
class publ {
public $tagging_line = "Scientists & Engineers are the real Geeks";
function display() {
echo $this->tagging_line."\n";
}
}
// SubClass
class subc extends publ {
function show(){
echo $this->tagging_line;
}
}
// Object Declaration
$obj= new subc;
// Scientists & Engineers are the real Geeks!
echo $obj->tagging_line."\n";
// Scientists & Engineers are the real Geeks!
$obj->display();
// Scientists & Engineers are the real Geeks!
$obj->show();
?>
로그인 후 복사

출력:

PHP의 공개 함수

예시 #2

수업 내부와 외부에서 공개 콘텐츠에 접근하는 예시입니다. 아래에서 나열된 구문을 확인하세요.

코드:

<?php
class Itemone
{
/**
* This is the INSIDE PROGRAMMING CODE because it is actually written INSIDE of the class.
*/
public $labelone;
public $priceone;
}
/**
* This is OUTSIDE PROGRAMMING CODE because it is actually written OUTSIDE of the class.
*/
$item = new Itemone();
$item->labelone = ' Phantam Drone - Mavic Pro ';
$item->priceone = 250.99;
echo $item->labelone;
/**
* Printing your variable value which contains string. $item is the public function variable declaration to store values accessing from Item function
*/
echo $item->priceone;
?>
로그인 후 복사

출력:

PHP의 공개 함수

예시 #3

또 다른 예입니다. 실제로 구문에는 프로그램에 대한 더 나은 이해를 돕기 위한 보호 변수도 포함되어 있습니다.

코드:

<?php
class Itemone
{
/**
* Here's the new INSIDE PROGRAMMING CODE and the Rules Which are to follow:
*
* 1. It will STOP ACCESS to the properties of it via $itemone->labelone and $itemone >priceone,
* with the help of the protected keyword.
* 2. FORCING the use of the public functions in the code.
* 3. The ONLY strings are now allowed OUT & IN of this class/classes for $labelone
* with the help of the getLabelone and setLabelone functions.
* 4. In OUT & IN of the class only floats are allowed now for $priceone
* by using getPriceone and setPriceone functions.
*/
protected $labelone = 'Unknown ItemONE'; // Rule 1 - protected Variable.
protected $priceone = 0.0; // Rule 1 - protected Variable.
public function getLabelone() { // Rule 2 - public function declaration.
return $this->labelone; // Rule 3 - string OUT for $labelone.
}
public function getPriceone() { // Rule 2 - public function declaration for Priceone.
return $this->priceone; // Rule 4 - The float OUT for $priceone.
}
public function setLabelone($labelone) // Rule 2 - public function declaration.
{
/**
* Make sure $labelone is a PHP string that can be used in a SORTING
* alogorithm, number, array, NOT a boolean or the object that can't be
* properly sorted -- AND to make sure that the getLabelone() function
* ALWAYS returns PHP string which is genuine.
*
* Using a RegExp would now improve this function, however, the main
* point is the one made above.
*/
if(is_string($labelone))
{
$this->labelone = (string)$labelone; // Rule 3 - string IN for $label.
}
}
public function setPriceone($priceone) // Rule 2 - public function.
{
/**
* Make sure $priceone is a PHP float value so that it can be used in a particular
* NUMERICAL CALCULATION. Do not accept string, array, boolean or
* some of the other object/objects that can't be included in a simple calculation.
* Now This will ensure that the getPriceone() function will ALWAYS returns
* genuine, authentic and also full-flavored PHP's number and nothing but.
*
* Checking the positive values may/might improve this one function,
* however, the main point is the one made above.
*/
if(is_numeric($priceone))
{
$this->priceone = (float)$priceone; // Rule 4 - float IN for $price.
}
}
}
?>
로그인 후 복사

장점es

아래에 설명된 공개 기능의 몇 가지 장점이 있습니다.

  • 전체 프로그램/프로젝트 어디에서나 액세스할 수 있습니다. 즉, 클래스의 Pubic 메서드나 PHP의 공용 함수는 클래스 외부나 내부 또는 하위 클래스에서 호출할 수 있다는 것입니다.
  • 클래스 외부는 물론, 클래스 내부에서도 다른 클래스에서 접근이 가능합니다.
  • 이 공개는 접근 기능에 아무런 제한이 없습니다. 이는 특정 객체의 공공 재산과 같습니다. 프로그램 내부 어디에서나 수정하거나 검색할 수 있습니다.
  • 공개 기능은 코드의 전체 의도를 표시/제공합니다.
  • 액세스된 경우에만 프로그램 전체에 공개됩니다.

규칙 및 규제

다음은 공적 기능에 대한 몇 가지 규칙 및 규정입니다.

  • Public Method/Function/Modifier/Keyword는 클래스 접근 내부는 물론, 클래스 외부에서도 제한 없이 호출 가능합니다.
  • 공개 함수/수정자는 공개 함수의 프로그래밍 코드가 해당 코드 명령을 실행하는 데 필요할 때 액세스해야 합니다. 그렇지 않으면 공개 함수는 아무 작업도 수행하지 않습니다.
  • 공개 함수를 사용하여 클래스 내에서 접근/유사합니다.

위 내용은 PHP의 공개 함수의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!