如果你在 8.4 上内化了这些,升级到 8.5 往往感觉像"添加一些好东西",而不是"把整个 PHP 写法现代化"。
所以这篇文章可以当作基线知识:如果你还没上 8.4,这是你为 8.5 做准备应该知道的——但不会变成完整的升级清单。
class Example { private bool $modified = false ; public string $foo = 'default value' { get { if ( $this -> modified ) { return $this -> foo . ' (modified)' ; } return $this -> foo ; } set ( string $value ) { $this -> foo = strtolower ( $value ) ; $this -> modified = true ;
class Rectangle { public function __construct ( public int $h , public int $w , ) { } public int $area { get => $this -> h * $this -> w ; } } $r = new Rectangle ( 4 , 5 ) ; echo $r -> area ; // 20
{ public string $username { set { $v = trim ( $value ) ;6ps.txzf-pay.com if ( $v === '' ) { throw new InvalidArgumentException ( 'Username cannot be empty.' ) ; } if ( strlen ( $v ) < 3 ) { throw new InvalidArgumentException ( 'Username is too short.' ) ; }
{ private ? array $cachedClaims = null ; public function __construct ( public string $jwt , ) { } public array $claims { get { if ( $this -> cachedClaims !== null ) { return $this -> cachedClaims ; } // 假设 parseJwt() 做签名检查、base64 解码等 $this -> cachedClaims = $this -> parseJwt ( $this -> jwt ) ; return $this -> cachedClaims ; }
1 PS C:\Users\yudong04> Get-WmiObject Win32_OperatingSystem 2 Get-WmiObject : 无效类 “Win32_OperatingSystem” 3 所在位置 行:1 字符: 1 4 + Get-WmiObject Win32_OperatingSystem 5 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 6 + CategoryInfo : InvalidType: (:) [ Get-WmiObject ], ManagementException 7
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand 8 9 PS C:\Users\yudong04> net stop winmgmt / y 10 Windows Management Instrumentation 服务正在停止. 11 Windows Management Instrumentation 服务已成功停止。 12 13 PS C:\Users\yudong04> winmgmt / resetrepository 14 WMI 存储库已重置 15 16 PS C:\Users\yudong04> Get-WmiObject Win32_OperatingSystem 17 18 19 SystemDirectory : C:\WINDOWS\system32 20 Organization : Online Game Dept 21 BuildNumber : 26100
var disks = new List<LocalDisk>(); 4 try 5 { 6 // Win32_DiskDrive: 物理磁盘 7 using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")) 8 using (var driveCollection = searcher.Get()) 9 {10 foreach (ManagementObject drive in driveCollection)11 {12 var diskInfo = new LocalDisk();13 14 // 1. 磁盘编号 PhysicalDriveN15 // Win32_DiskDrive.DeviceID 一般为 "\\.\PHYSICALDRIVE0"16 var deviceId = (drive["DeviceID"] as string) ?? string.Empty;17 var diskNumber = ParsePhysicalDriveNumber(deviceId);18 diskInfo.Number = diskNumber;19 20 // 2. 序列号 (不同厂商格式不统一;有时需要 Win32_PhysicalMedia)21
diskInfo.SerialNumber = (drive["SerialNumber"] as string)?.Trim() ?? string.Empty;22 23 // 3. DeviceName24 diskInfo.DeviceName = (drive["Model"] as string)?.Trim() ?? string.Empty;25 26 // 4. 只读/在线状态(WMI 并没有非常标准的字段,这里用粗略映射) x8xoty.txzf-pay.com27 // Win32_DiskDrive.Status: "OK" / "Error" / "Degraded" ...28 diskInfo.IsOffline = GetOffline(diskNumber);29 30 5s00uz.txzf-pay.com
// 没有直接 readonly 标记,先默认为 false,31 // 如需更精确可以通过 Win32_Volume 或 DeviceIoControl 获取。32 diskInfo.IsReadOnly = GetReadonly(diskNumber);33 34 // 5. 总线类型(没有 STORAGE_BUS_TYPE 枚举,使用 InterfaceType 粗略映射)35 var interfaceType = (drive["InterfaceType"] as string)?.Trim();36 diskInfo.BusType = MapBusType(interfaceType, diskInfo.DeviceName);37 38 磁盘容量 (字节 -> GB)39 // Win32_DiskDrive.Size 为字节数(string)40 if (drive["Size"] != null && long.TryParse(drive["Size"].ToString(), out long sizeBytes))41 {42 diskInfo.DiskSize = sizeBytes;43 }44 45 // 7. 获取挂载点及已用容量,通过 3 张 WMI 关联表:46 // Win32_DiskDrive -> Win32_DiskDriveToDiskPartition -> Win32_DiskPartition ->47 // Win32_LogicalDiskToPartition -> Win32_LogicalDisk48 FillMountPathsAndUsedSize(diskInfo, drive);49 disks.Add(diskInfo);50 51 diskInfo.Tag = GetVolumeLabel(diskInfo.MountPaths.FirstOrDefault());52 }53 }54 55 return OperateResult<List<LocalDisk>>.ToSuccess(disks.OrderBy(i => i.Number).ToList());56 }
/// </summary> 4 /// <param name="diskNumber">磁盘编号</param> 5 /// <param name="volumeMaps"></param> 6 /// <returns></returns> 7 private OperateResult<LocalDisk> GetDiskInfoByDiskNumber(int diskNumber, Dictionary<int, List<string>> volumeMaps) 8 { 9 //逐个尝试 PhysicalDrive0..N10
string physicalDrive = @"\\.\PhysicalDrive" + diskNumber;11 IntPtr hDisk = CreateFile(12 physicalDrive,13 GENERIC_READ,14 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,15 IntPtr.Zero,16 OPEN_EXISTING,17 nxdpxg.txzf-pay.com 0,18 IntPtr.Zero);19 try20 {21 // 不存在这个物理盘(或者无权限),忽略此异常22 if (hDisk == INVALID_HANDLE_VALUE)23 {24 3wk.txzf-pay.com return OperateResult<LocalDisk>.ToSuccess();25
}26 var diskInfo = new LocalDisk();27 diskInfo.Number = diskNumber;28 29 //获取磁盘基础信息30 var getDiskPropertiesResult = GetDiskProperties(hDisk);31 if (!getDiskPropertiesResult.Success)32 {33 return OperateResult<LocalDisk>.ToError($"Get disk {physicalDrive} properties failed, {getDiskPropertiesResult.Message}", getDiskPropertiesResult.Exception, getDiskPropertiesResult.Code);34 }35 vkuj.txzf-pay.com var diskProperties = getDiskPropertiesResult.Data;36 diskInfo.SerialNumber = diskProperties.SerialNumber;37 diskInfo.DeviceName = diskProperties.DeviceName;38 diskInfo.BusType = diskProperties.BusType;39 40 //是否只读/联机 nhyrak.txzf-pay.com41 var diskAttributesResult = GetDiskAttributes(hDisk);42 if (!diskAttributesResult.Success)43 {44 return OperateResult<LocalDisk>.ToError($"Get disk {diskProperties.DeviceName} attributes failed, {diskAttributesResult.Message}", diskAttributesResult.Exception, diskAttributesResult.Code);45 }46 zrv.txzf-pay.com
var diskStorageAttributes = diskAttributesResult.Data;47 diskInfo.IsReadOnly = diskStorageAttributes.IsReadOnly;48 diskInfo.IsOffline = diskStorageAttributes.IsOffline;49 50 //磁盘容量51 var getDiskSizeResult = GetDiskSize(hDisk);52 diskInfo.DiskSize = getDiskSizeResult.Data;53 54 //获取分区信息55 var partitionInfoResult = GetPartitionInfo(hDisk);56 if (!partitionInfoResult.Success)57 {58 nhyrak.txzf-pay.com return OperateResult<LocalDisk>.ToError($"Get disk {diskProperties.DeviceName} partition failed, {partitionInfoResult.Message}", partitionInfoResult.Exception, partitionInfoResult.Code);59 }60 var diskPartitionInfo = partitionInfoResult.Data;61 diskInfo.PartitionStyle = (DiskPartitionStyle)diskPartitionInfo.PartitionStyle;62 diskInfo.PartitionCount = diskPartitionInfo.PartitionCount;63
//基础数据区分大小64 pvrgnm.txzf-pay.com diskInfo.DiskAllocateSize = diskPartitionInfo.Partitions.FirstOrDefault(i => i.PartitionType.ToUpper() == "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7")?.PartitionLength ?? 0;65 66 //挂载路径 www.stnj.net67 if (volumeMaps.TryGetValue(diskNumber, out var mounts) && mounts != null)68 {69 diskInfo.MountPaths = mounts;70 }71 stnj.net //获取卷标名称72 if (diskInfo.MountPaths.Any())73 {74 //通过任意一个mountPath获取75 var mountPath = diskInfo.MountPaths.First();76 var getVolumeInfoResult = GetVolumeInfo(mountPath);77 diskInfo.Tag = getVolumeInfoResult.Data?.VolumeLabel ?? string.Empty;78 diskInfo.FileSystemType = getVolumeInfoResult.Data?.FileSystemType ?? string.Empty;79 }80 //磁盘已使用大小81 if (diskInfo.MountPaths.Any())82 {83 long diskUsedSize = 0L;84 //通过所有mountPath相加,获取磁盘已使用大小85 foreach (var mountPath in diskInfo.MountPaths)86 {87 var usageByMountPathResult = GetDiskSizeUsageByMountPath(mountPath);88 9vr7.txzf-pay.com diskUsedSize += usageByMountPathResult.Data?.UsedBytes ?? 0;89 }90 diskInfo.DiskUsedSize = diskUsedSize;91 }92 return OperateResult<LocalDisk>.ToSuccess(diskInfo);93 }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3109436/ ,如需转载,请注明出处,否则将追究法律责任。