{"id":782,"date":"2025-08-04T16:16:02","date_gmt":"2025-08-04T07:16:02","guid":{"rendered":"https:\/\/nexive.tech\/?p=782"},"modified":"2025-08-04T16:16:02","modified_gmt":"2025-08-04T07:16:02","slug":"%e3%80%90%e3%82%b5%e3%82%af%e3%83%83%e3%81%a8%e8%a7%a3%e8%aa%ac%e3%80%91php-static%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%83%bb%e3%83%97%e3%83%ad%e3%83%91%e3%83%86%e3%82%a3%e3%81%ae%e4%bd%bf%e3%81%84","status":"publish","type":"post","link":"https:\/\/nexive.tech\/?p=782","title":{"rendered":"\u3010\u30b5\u30af\u30c3\u3068\u89e3\u8aac\u3011PHP static\u30e1\u30bd\u30c3\u30c9\u30fb\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP\u306estatic\u30e1\u30bd\u30c3\u30c9\u30fb\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u57fa\u672c\u69cb\u6587\u3068\u52d5\u4f5c\u539f\u7406\u3092\u89e3\u8aac<\/li>\n\n\n\n<li>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4e0d\u8981\u3067\u5229\u7528\u3067\u304d\u308b\u5834\u9762\u3068\u8a2d\u8a08\u4e0a\u306e\u6ce8\u610f\u70b9\u3092\u7d39\u4ecb<\/li>\n\n\n\n<li>self\/parent\/static\u306b\u3088\u308b\u53c2\u7167\u306e\u9055\u3044\u3068\u6d3b\u7528\u4f8b\u3092\u63d0\u793a<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. PHP\u306estatic\u30e1\u30bd\u30c3\u30c9\u30fb\u30d7\u30ed\u30d1\u30c6\u30a3\u3068\u306f\uff1f<\/h2>\n\n\n\n<p>PHP\u306b\u304a\u3051\u308b<code>static<\/code>\u306f\u3001\u30af\u30e9\u30b9\u306b\u5c5e\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3084\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u305b\u305a\u306b\u5229\u7528\u53ef\u80fd\u306b\u3059\u308b\u305f\u3081\u306e\u30ad\u30fc\u30ef\u30fc\u30c9\u3067\u3059\u3002Java\u3084C#\u306a\u3069\u306e\u4ed6\u8a00\u8a9e\u3068\u540c\u69d8\u3001\u5171\u901a\u7684\u306a\u51e6\u7406\u3084\u5b9a\u6570\u7684\u306a\u5024\u3092\u5b9a\u7fa9\u3059\u308b\u5834\u9762\u3067\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4e3b\u306a\u7528\u9014\u3068\u3057\u3066\u306f\u4ee5\u4e0b\u304c\u6319\u3052\u3089\u308c\u307e\u3059\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30ed\u30b0\u51fa\u529b\u3084\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u95a2\u6570\u306a\u3069\u3001\u72b6\u614b\u3092\u6301\u305f\u306a\u3044\u51e6\u7406\u306e\u63d0\u4f9b<\/li>\n\n\n\n<li>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u751f\u6210\u524d\u306e\u8a2d\u5b9a\u5024\u306e\u4fdd\u6301<\/li>\n\n\n\n<li>\u30af\u30e9\u30b9\u3054\u3068\u306e\u5171\u6709\u30ea\u30bd\u30fc\u30b9\uff08\u4f8b\uff1a\u63a5\u7d9a\u6570\u30ab\u30a6\u30f3\u30c8\u306a\u3069\uff09\u306e\u7ba1\u7406<\/li>\n<\/ul>\n\n\n\n<p>\u5b9f\u52d9\u3067\u306f\u3001\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u30af\u30e9\u30b9\u3084\u5b9a\u6570\u7ba1\u7406\u30af\u30e9\u30b9\u306a\u3069\u306b\u591a\u7528\u3055\u308c\u307e\u3059\u3002\u305f\u3060\u3057\u3001static\u306a\u8a2d\u8a08\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u306e\u67d4\u8edf\u6027\u3092\u640d\u306a\u3046\u53ef\u80fd\u6027\u3082\u3042\u308b\u305f\u3081\u3001\u4f7f\u7528\u306b\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u8a73\u7d30\u89e3\u8aac<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u69cb\u6587\uff1astatic\u30e1\u30bd\u30c3\u30c9\u3068\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u5b9a\u7fa9\u3068\u547c\u3073\u51fa\u3057<\/h3>\n\n\n\n<p>\u9759\u7684\u30e1\u30bd\u30c3\u30c9\u30fb\u30d7\u30ed\u30d1\u30c6\u30a3\u306f<code>static<\/code>\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u4ed8\u3051\u3066\u5b9a\u7fa9\u3057\u3001\u30af\u30e9\u30b9\u540d\u307e\u305f\u306fself\u30ad\u30fc\u30ef\u30fc\u30c9\u3067\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>class MathUtil {\n    public static int $count = 0; \/\/ \u9759\u7684\u30d7\u30ed\u30d1\u30c6\u30a3\n\n    public static function add(int $a, int $b): int {\n        self::$count++;\n        return $a + $b;\n    }\n}\n\n\/\/ \u547c\u3073\u51fa\u3057\u4f8b\n$result = MathUtil::add(3, 5);\necho $result; \/\/ 8\n\necho MathUtil::$count; \/\/ 1\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">MathUtil<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">public<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$count<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ \u9759\u7684\u30d7\u30ed\u30d1\u30c6\u30a3<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">public<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">function<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">add<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$a<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$b<\/span><span style=\"color: #D4D4D4\">): <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #9CDCFE\">$count<\/span><span style=\"color: #D4D4D4\">++;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$a<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #9CDCFE\">$b<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u547c\u3073\u51fa\u3057\u4f8b<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$result<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #4EC9B0\">MathUtil<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">add<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$result<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ 8<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">MathUtil<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #9CDCFE\">$count<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ 1<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u9759\u7684\u30d7\u30ed\u30d1\u30c6\u30a3\u306f\u3001\u5168\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u5171\u901a\u306e\u5024\u3092\u6301\u3061\u307e\u3059\u3002\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3054\u3068\u306e\u72b6\u614b\u3092\u6271\u3044\u305f\u3044\u5834\u5408\u306b\u306f\u4e0d\u9069\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">self \/ static \/ parent \u306e\u9055\u3044\u3068\u4f7f\u3044\u5206\u3051<\/h3>\n\n\n\n<p>\u7d99\u627f\u6642\u306bstatic\u306a\u8981\u7d20\u3092\u53c2\u7167\u3059\u308b\u969b\u3001<code>self::<\/code>\u3068<code>static::<\/code>\u3067\u6319\u52d5\u304c\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>class Base {\n    public static function who() {\n        echo \"Base\\n\";\n    }\n    public static function callSelf() {\n        self::who();\n    }\n    public static function callStatic() {\n        static::who();\n    }\n}\n\nclass Child extends Base {\n    public static function who() {\n        echo \"Child\\n\";\n    }\n}\n\nChild::callSelf();   \/\/ Base\nChild::callStatic(); \/\/ Child\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Base<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">public<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">function<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">who<\/span><span style=\"color: #D4D4D4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;Base<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">public<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">function<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">callSelf<\/span><span style=\"color: #D4D4D4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">who<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">public<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">function<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">callStatic<\/span><span style=\"color: #D4D4D4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">who<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Child<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">extends<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Base<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">public<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">function<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">who<\/span><span style=\"color: #D4D4D4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;Child<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #4EC9B0\">Child<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">callSelf<\/span><span style=\"color: #D4D4D4\">();   <\/span><span style=\"color: #6A9955\">\/\/ Base<\/span><\/span>\n<span class=\"line\"><span style=\"color: #4EC9B0\">Child<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">callStatic<\/span><span style=\"color: #D4D4D4\">(); <\/span><span style=\"color: #6A9955\">\/\/ Child<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><code>self::<\/code>\u306f\u5b9a\u7fa9\u5143\u306e\u30af\u30e9\u30b9\uff08\u3053\u306e\u5834\u5408\u306fBase\uff09\u3092\u57fa\u6e96\u306b\u3001<code>static::<\/code>\u306f\u5b9f\u884c\u6642\u306e\u30af\u30e9\u30b9\uff08\u3053\u306e\u5834\u5408\u306fChild\uff09\u3092\u57fa\u6e96\u306b\u89e3\u6c7a\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u3092<strong>Late Static Binding\uff08\u9045\u5ef6\u9759\u7684\u675f\u7e1b\uff09<\/strong>\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">static\u5909\u6570\u3068\u306e\u6df7\u540c\u306b\u6ce8\u610f<\/h3>\n\n\n\n<p><code>static<\/code>\u306f\u30e1\u30bd\u30c3\u30c9\u30fb\u30d7\u30ed\u30d1\u30c6\u30a3\u4ee5\u5916\u306b\u3082\u3001\u95a2\u6570\u5185\u5909\u6570\u306b\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7528\u9014\u3068\u30b9\u30b3\u30fc\u30d7\u304c\u7570\u306a\u308b\u305f\u3081\u6df7\u540c\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>function counter() {\n    static $i = 0; \/\/ \u521d\u56de\u306e\u307f\u521d\u671f\u5316\n    $i++;\n    return $i;\n}\n\necho counter(); \/\/ 1\n\necho counter(); \/\/ 2\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">function<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">counter<\/span><span style=\"color: #D4D4D4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$i<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ \u521d\u56de\u306e\u307f\u521d\u671f\u5316<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$i<\/span><span style=\"color: #D4D4D4\">++;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$i<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">counter<\/span><span style=\"color: #D4D4D4\">(); <\/span><span style=\"color: #6A9955\">\/\/ 1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">counter<\/span><span style=\"color: #D4D4D4\">(); <\/span><span style=\"color: #6A9955\">\/\/ 2<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u95a2\u6570\u5185\u306e<code>static<\/code>\u5909\u6570\u306f\u547c\u3073\u51fa\u3057\u9593\u3067\u72b6\u614b\u3092\u4fdd\u6301\u3057\u7d9a\u3051\u307e\u3059\u3002\u30af\u30e9\u30b9\u5916\u3067\u306e\u72b6\u614b\u4fdd\u6301\u306b\u5229\u7528\u3055\u308c\u308b\u30b1\u30fc\u30b9\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u3088\u304f\u3042\u308b\u30df\u30b9\u30fb\u8aa4\u89e3\u30fb\u843d\u3068\u3057\u7a74<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9759\u7684\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u7d4c\u7531\u3067\u64cd\u4f5c\u3057\u3066\u3057\u307e\u3046<\/strong><br>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304b\u3089\u3067\u3082\u30a2\u30af\u30bb\u30b9\u306f\u53ef\u80fd\u3067\u3059\u304c\u3001\u53ef\u8aad\u6027\u30fb\u8a2d\u8a08\u4e0a\u306e\u89b3\u70b9\u304b\u3089\u975e\u63a8\u5968\u3067\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u304cself\u53c2\u7167\u3067\u52b9\u304b\u306a\u3044<\/strong><br>\u7d99\u627f\u3092\u610f\u8b58\u3057\u305f\u8a2d\u8a08\u3067\u306f\u3001<code>self::<\/code>\u3088\u308a\u3082<code>static::<\/code>\u3092\u4f7f\u3046\u307b\u3046\u304c\u52d5\u7684\u306a\u6319\u52d5\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u72b6\u614b\u3092\u6301\u3064\u51e6\u7406\u306bstatic\u3092\u4f7f\u3063\u3066\u3057\u307e\u3046<\/strong><br>static\u306b\u3059\u308b\u3068\u4f9d\u5b58\u95a2\u4fc2\u3092\u96a0\u853d\u3057\u3084\u3059\u304f\u3001\u30c6\u30b9\u30c8\u3084\u62e1\u5f35\u6027\u304c\u640d\u306a\u308f\u308c\u307e\u3059\u3002\u72b6\u614b\u3092\u6301\u3064\u51e6\u7406\u306f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5074\u306b\u6301\u305f\u305b\u308b\u306e\u304c\u57fa\u672c\u3067\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30af\u30e9\u30b9\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u521d\u671f\u5316\u9806\u306b\u6ce8\u610f<\/strong><br>PHP 8.1\u4ee5\u964d\u3001\u521d\u671f\u5316\u9806\u30fb\u578b\u5236\u7d04\u304c\u53b3\u5bc6\u306b\u306a\u3063\u3066\u304a\u308a\u3001\u672a\u521d\u671f\u5316\u306estatic\u30d7\u30ed\u30d1\u30c6\u30a3\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u307e\u3068\u3081<\/h2>\n\n\n\n<p>PHP\u306estatic\u30e1\u30bd\u30c3\u30c9\u30fb\u30d7\u30ed\u30d1\u30c6\u30a3\u306f\u3001\u5171\u901a\u51e6\u7406\u306e\u6574\u7406\u3084\u52b9\u7387\u7684\u306a\u30ea\u30bd\u30fc\u30b9\u7ba1\u7406\u306b\u5f79\u7acb\u3061\u307e\u3059\u304c\u3001\u67d4\u8edf\u6027\u3068\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30af\u30e9\u30b9\u540d\u307e\u305f\u306fself\/static\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u70b9\u3092\u6b63\u3057\u304f\u7406\u89e3\u3059\u308b\u3053\u3068<\/li>\n\n\n\n<li>\u7d99\u627f\u3092\u610f\u8b58\u3059\u308b\u5834\u5408\u306f<code>static::<\/code>\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068<\/li>\n\n\n\n<li>\u72b6\u614b\u3092\u6301\u305f\u305b\u308b\u7528\u9014\u3067\u306f\u5b89\u6613\u306b\u4f7f\u308f\u306a\u3044\u3053\u3068<\/li>\n<\/ul>\n\n\n\n<p>\u8a2d\u8a08\u610f\u56f3\u306b\u5408\u308f\u305b\u3066\u9069\u5207\u306b\u4f7f\u3044\u5206\u3051\u308b\u3053\u3068\u3067\u3001\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u30b3\u30fc\u30c9\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8 2. PHP\u306estatic\u30e1\u30bd\u30c3\u30c9\u30fb\u30d7\u30ed\u30d1\u30c6\u30a3\u3068\u306f\uff1f PHP\u306b\u304a\u3051\u308bstatic\u306f\u3001\u30af\u30e9\u30b9\u306b\u5c5e\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3084\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u305b\u305a\u306b\u5229\u7528\u53ef\u80fd\u306b\u3059\u308b\u305f\u3081\u306e\u30ad\u30fc\u30ef\u30fc\u30c9\u3067\u3059\u3002Java\u3084C#\u306a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":702,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-782","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=782"}],"version-history":[{"count":2,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/782\/revisions"}],"predecessor-version":[{"id":875,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/782\/revisions\/875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/media\/702"}],"wp:attachment":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}