[英]PHP and DRUPAL, Cannot save values in a static variable and pass through function
[英]Share values between to static function with our pass through arguments
我想在一個類的兩個靜態函數之間共享一個對象,且不帶通過參數。 例如,在我的類中,必須使用static function ProfileRegistration()
來通過參數獲取對象,而我想調用另一個函數Format()
來處理相同的對象。 我想在without
再次傳遞object
without
調用Format()
函數
我不知道在函數之間共享值,所以我將其作為參數傳遞。 如何避免呢?
class SMSList {
var $objLogin;
public function __construct()
{
}
public static function ProfileRegistration($objLogin)
{
$objself=new self();
$objself->objLogin=$objLogin;
$obj=new SMSMessage();
if($obj->profile_registration_sms_status==1)
{
$msg=self::Format($obj->profile_registration_sms,$objself-objLogin);
return SMS::sendSMS($objself->objLogin->mobile,$msg);
}
}
public function Format($message,$objLogin)
{
$message=str_replace('#NAME#',$objLogin->contact_person,$message);
$message=str_replace('#COMP_NAME#',$objLogin->companyname,$message );
$message=str_replace('#MOBILE#',$objLogin->mobile,$message);
$message=str_replace('#CITY#',$objLogin->city,$message);
$message=str_replace('#EMAILID#',$objLogin->emailid,$message);
return $message;
}
}
使用靜態變量解決此問題。 這里的代碼示例
class SMSList {
public static $objLogin;
public function __construct()
{
}
public static function ProfileRegistration($objLogin)
{
self::$objLogin=$objLogin;
$obj=new SMSMessage();
if($obj->profile_registration_sms_status==1)
{
$msg=self::Format($obj->profile_registration_sms);
return SMS::sendSMS( self::$objLogin->mobile,$msg);
}
} public function Format($message)
{
$message=str_replace('#NAME#',self::$objLogin->contact_person,$message);
$message=str_replace('#COMP_NAME#',self::$objLogin->companyname,$message );
$message=str_replace('#MOBILE#',self::$objLogin->mobile,$message);
$message=str_replace('#CITY#',self::$objLogin->city,$message);
$message=str_replace('#EMAILID#',self::$objLogin->emailid,$message);
return $message;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.