php 中 static::$var 的用法

本文通过两个实例详细解析了PHP中static关键字的用法,特别是static::$var的特性,展示了如何在继承类中引用父类及当前类的静态成员变量。

       最近看到 php 中使用语法如 static::$var ,有点懵,于是百度后发现,static::$var 会强制使用当前调用对象所属类中的变量 $var 的值,还是看下下面的2个例子吧。

class a{
   static protected $test="class a";
   public function static_test(){
     echo static::$test; //class b
     echo self::$test;   //class a
   }
}
class b extends a{
   static protected $test="class b";
}

$obj = new b();
$obj->static_test();
class par{
    const v='A';
    public function f1()
    {
        echo static::v;//静态绑定static::
    }
    public function f2()
    {
        echo self::v;
    }
}
class child extends par{
    const v='B';
}
$b=new child();
$b->f1();  //result: 'B'
echo '<br>';
$b->f2();  //result: 'A'



<?php namespace ChatGPT; use Firebase\JWT\JWT; class deepSeek { protected static $model = 'deepseek-reasoner'; protected static $apiKey = ''; protected static $temperature = 0.9; protected static $max_tokens = 3000; protected static $apiUrl = 'https://api.deepseek.com/v1/chat/completions'; // 修正API路径,添加/v1 /** * @param string $apiKey * @param string $model * @param string $temperature */ public function __construct($apiKey = '', $model = '', $temperature = '', $max_tokens = 3000) { if ($model) { self::$model = $model; } if ($temperature) { self::$temperature = $temperature; } if ($max_tokens) { // 添加范围验证 self::$max_tokens = min(max(1, $max_tokens), 8192); } self::$apiKey = $apiKey; } /** * @param $message * @param $callback * @return array|mixed */ public function sendText($message = [], $callback = null) { $post = [ 'model' => self::$model, 'messages' => $message, 'temperature' => floatval(self::$temperature), 'max_tokens' => intval(self::$max_tokens), 'stream' => false ]; $debug_log = '../app/web/controller/debug.log'; $result = $this->httpRequest(self::$apiUrl, $post, $callback); file_put_contents($debug_log, '$deepseek $result1=' . var_export($result,true), FILE_APPEND); return $this->handleError($result); } /** * @param $text * @return false|string */ public function getEmbedding($text) // 注意:方法名拼写应为getEmbedding { if (mb_strlen($text) > 512) { return ''; } $url = 'https://api.deepseek.com/v1/embeddings'; // 修正为DeepSeek的embedding接口 $post = [ 'model' => self::$model, // 添加model参数 'input' => $text // 修正参数名为input ]; 这是代码
03-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值