検索

php5 マジック関数、マジック定数
http://www.loveiso.com/article/11-06-11/48.html より転載

魔法の関数

1. __construct()
はオブジェクトをインスタンス化するときに呼び出されます。
__construct とクラス名と関数名の関数が同時に存在する場合、__construct が呼び出され、もう一方は呼び出されません。

2. __destruct()
オブジェクトが削除されるか、オブジェクトの操作が終了するときに呼び出されます。

3. __call()
オブジェクトは特定のメソッドを呼び出します。
メソッドが存在する場合は直接呼び出されます。
存在しない場合は __call 関数が呼び出されます。

4. __get()
オブジェクトの属性を読み取るとき、
属性が存在する場合は属性値が直接返され、
存在しない場合は __get 関数が呼び出されます。

5. __set()
オブジェクトの属性を設定する場合、
属性が存在する場合は値が直接割り当てられ、
存在しない場合は __set 関数が呼び出されます。

6. __toString()
は、オブジェクトを印刷するときに呼び出されます。 echo $obj; または print $obj;

など。 __clone()
オブジェクトのクローンを作成するときに呼び出されます。例: $t=new Test();$t1=clone $t;

8。 __sleep()
はシリアル化の前に呼び出されます。オブジェクトが比較的大きく、シリアル化する前にいくつかのものを削除したい場合は、この関数を検討できます。

9. __wakeup()
は、シリアル化解除時に呼び出され、オブジェクトの初期化作業を実行します。

10. __isset()
は、オブジェクトの属性が存在するかどうかを確認するときに呼び出されます。例: isset($c->name)。

11. __unset()
オブジェクトのプロパティの設定を解除するときに呼び出されます。例: unset($c->name)。

12. __set_state()
は、var_export が呼び出されたときに呼び出されます。 __set_state の戻り値を var_export の戻り値として使用します。

13. __autoload()
オブジェクトをインスタンス化する際、対応するクラスが存在しない場合、このメソッドが呼び出されます。

魔法定数

1. __LINE__
ファイル内の現在の行番号を返します。

2. __FILE__
ファイルのフルパスとファイル名を返します。インクルード ファイルで使用された場合は、インクルード ファイル名を返します。 PHP 4.0.2 以降、__FILE__ には常に絶対パスが含まれますが、それより前のバージョンには相対パスが含まれる場合がありました。

3. __FUNCTION__
関数名を返します (PHP 4.3.0 の新機能)。 PHP 5 以降、この定数は定義されたとおりの関数名を返します (大文字と小文字は区別されます)。 PHP 4 では、この値は常に小文字です。

4. __CLASS__
クラスの名前を返します (PHP 4.3.0 の新機能)。 PHP 5 以降、この定数は定義されたときのクラスの名前を返します (大文字と小文字は区別されます)。 PHP 4 では、この値は常に小文字です。

5. __METHOD__
クラスのメソッド名を返します (PHP 5.0.0 で新しく追加されました)。定義されたとおりのメソッド名を返します (大文字と小文字が区別されます)。




(1) マジックメソッドの最初の紹介
Php5.0 はリリース以来、多くのオブジェクト指向機能、特に使いやすい Magic を多数提供してきました。これらの魔法のメソッドを使用すると、コーディングを簡素化し、システムをより適切に設計できるようになります。今日はphp5.0が提供するマジックメソッドについて学びます。


3,__get() は、存在しないプロパティを読み取ろうとしたときに呼び出されます。
オブジェクトに存在しないプロパティを読み取ろうとすると、PHP はエラー メッセージを表示します。クラスに __get メソッドを追加すると、この関数を使用して Java のリフレクションと同様のさまざまな操作を実装できます。

class Test
{
public function __get($key)
{
echo $key . " が存在しません";
}
}


$t = new Test();
echo $t->name;

は出力します:
name が存在しません
4,__set() の場合存在しないプロパティに値を書き込むときに呼び出されます。 class Test
{
public function __set($key,$value)
{
echo 'right'.$key . "With value".$value;
}
}


$t = new Test();
$t->name = "aninggo";

は出力します:
add value to name aninggo
5,__call() このメソッドは、オブジェクトに存在しないメソッドを呼び出そうとしたときに呼び出されます。 class Test
{
public function __call($Key, $Args)
{
echo "呼び出したい {$Key} メソッドは存在しません。渡されたパラメータは次のとおりです:" . print_r ($Args, true);
}
}

$t = new Test();
$t->getName(aning,go);
プログラムは次のように出力します:
呼び出したい getName メソッドは存在しません。パラメータは次のとおりです: Array
(
[0] => aning
[1] => go
)

6,__toString() はオブジェクトを出力するときに使用されます。
は、Java の toString メソッドに似ています。オブジェクトを直接出力する場合は、この関数

class Test
{
public function __toString()
{
return " を呼び出します。 Print Test";
}
}


$t = new Test();

echo $t;
実行 echo $t;複製されると、$t->__toString(); が呼び出されて出力されます
Print Test

7, __clone() オブジェクトが複製されると、

class と呼ばれますTest
{

public function __clone()
{
echo "コピーされました!";
}
}

$t = new Test ( );
$t1 = clone $t;

プログラム出力:
コピーされました

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Apple iPhone 16 Pro as Leica smartphone: Leica subsidiary confirms camera grip with official Leica appApple iPhone 16 Pro as Leica smartphone: Leica subsidiary confirms camera grip with official Leica appJun 13, 2024 pm 08:52 PM

LeicareleasedtheLeicaLuxcameraappfortheAppleiPhoneafewdaysago.However,theappwasnotdevelopedbyLeica,butbyFjorden.ThecompanyhasbeenknownprimarilyforitscameragripsfortheiPhoneandwasacquiredbyLeicainDecember2023.Fo

Remote 3 universal remote control comes with touchscreen, but without subscription or server obligationRemote 3 universal remote control comes with touchscreen, but without subscription or server obligationJun 14, 2024 am 09:13 AM

SincethedemiseofLogitech'spopularHarmonyremotecontrols,themarketforhigh-qualityuniversalremotecontrolshasbeenfragmentedatbest.UnfoldedCircleaimstoavoidthefateoftheHarmonyUltimatebyeliminatinganyserverobligationsorsubs

Samsung Galaxy S24 Ultra gains E Ink secondary display via inventive protective caseSamsung Galaxy S24 Ultra gains E Ink secondary display via inventive protective caseJun 14, 2024 am 10:44 AM

Anintriguingthird-partycasefortheGalaxyS24Ultra(curr.$1,099.99onAmazon)hasappearedonmarketplaceslikeAliExpress.Astheimagesthroughoutthisarticleshow,thecasehasasimplesiliconeconstruction.However,italsocontainsanEInkd

Electric Ferraris will boast an \'authentic\' growl, claims marketing executiveElectric Ferraris will boast an \'authentic\' growl, claims marketing executiveJun 14, 2024 am 10:26 AM

In2012,theeminentautomotivejournalistJeremyClarksonstatedthattheLamborghiniAventador,whichhewasreviewingatthetime,wouldbeamongthelastcarstofeatureanaturallyaspiratedV12.Morethanadecadelater,V12-poweredsupercarsareal

Light Phone 3 launches with 50% discount, monochrome OLED and minimalist designLight Phone 3 launches with 50% discount, monochrome OLED and minimalist designJun 13, 2024 pm 10:18 PM

WhiletheLightPhone2from2018wasstillequippedwithaneconomicale-inkdisplay,theLightPhone3usesanOLEDdisplaythatcanonlydisplaygrayscale.Thereasonfortheswitchtothe3.92-inchOLEDpanelwithitsresolutionof1,240x1,080isth

Audio-Technica unveils ATH-S300BT wireless headphones with hear-through noise-cancellation, 90 hours battery life, and multipoint pairingAudio-Technica unveils ATH-S300BT wireless headphones with hear-through noise-cancellation, 90 hours battery life, and multipoint pairingJun 14, 2024 am 09:46 AM

Audio-TechnicahasunveiledtheATH-S300BTwirelessheadphoneswithhear-throughnoise-cancellation,multipointpairing,and90hoursofbatterylife.Thenoise-cancellingfeaturehasthreemodes:off,on,andhear-through,whereambientsoundscanbehea

First complete PCIe 7.0 IP solution presented by Synopsys coming to market in 2025 for HPCs and AI supercomputersFirst complete PCIe 7.0 IP solution presented by Synopsys coming to market in 2025 for HPCs and AI supercomputersJun 14, 2024 am 09:19 AM

Backin2022,whenPCIe7.0wasstartingtotakeshapeasafuturestandard,PCIe5.0wasjusthittingtheservermarketsandtheprospectofseeingPCIe6.0devicesavailableanytimesoon,letalonePCIe7.0ones,wasquitefar-fetched.CurrentlyPCIe5

Jabra discontinues Elite wireless earbuds lineupJabra discontinues Elite wireless earbuds lineupJun 14, 2024 am 09:39 AM

TheJabraEliteserieshascometoanend,withGNCEOPeterKarlstromerannouncingthediscontinuationofthewirelessearbudswithinthelineupinapressrelease.Petersaysthatthechangingmarketdynamicsandhowchallengingithasbecometogetas

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)