很多站长朋友们都不太清楚php变量属性值,今天小编就来给大家整理php变量属性值,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP如何修改和获取private变量的值 2、 PHP的变量如何给抽象类中属性符初值 3、 PHP变量名、变量值、类型 PHP如何修改和获取private变量的值//__get()方法用来获取私有属性
private function __get($property_name)
{
if(isset($this->$property_name))
{
return($this->$property_name);
}else
{
return(NULL);
}
}
//__set()方法用来设置私有属性
private function __set($property_name, $value)
{
$this->$property_name = $value;
}
有了这2个方法以后,就可以直接执行:
echo $instance->$property
或 $instance->$property = “a”;
来获取和修改private变量的值了,如果没有手动添加__get();和__set();方法则会报错,
因为我们要访问的是私有变量。
希望可以采纳,谢谢。
PHP的变量如何给抽象类中属性符初值因为抽象类不能被实例化,所以需要通过继承的方式为属性赋值:
前提是抽象类中需要赋值的属性不能被private修饰
<?php
abstract class AbstractClass
{
public $a = 1;
abstract protected function getValue();
// 普通方法(非抽象方法)
public function printOut() {
echo $this->a . "\n";
}
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {}
}
$b = new ConcreteClass1();
$b->a = 2;
$b->printOut();
?>
PHP变量名、变量值、类型变量名 =》 zval
变量值 =》zend_value
问题:
引用计数
变量传递,变量赋值
变量的基础结构
变量值:zend_value
typedef union _zend_value {
zend_long lval; /* long value */
double dval; /* double value */
zend_refcounted *counted;
zend_string *str;
zend_array *arr;
zend_object *obj;
zend_resource *res;
zend_reference *ref;
zend_ast_ref *ast;
zval *zv;
void *ptr;
zend_class_entry *ce;
zend_function *func;
struct {
uint32_t w1;
uint32_t w2;
} ww;
} zend_value;
变量名:_zval
typedef struct _zval_struct zval;
struct _zval_struct {
zend_value value; /* value */
union {
struct {
ZEND_ENDIAN_LOHI_4(
zend_uchar type, /* active type */
zend_uchar type_flags,
zend_uchar const_flags,
zend_uchar reserved) /* call info for EX(This) */
} v;
uint32_t type_info;
} u1;
union {
uint32_t var_flags;
uint32_t next; /* hash collision chain */
uint32_t cache_slot; /* literal cache slot */
uint32_t lineno; /* line number (for ast nodes) */
uint32_t num_args; /* arguments number for EX(This) */
uint32_t fe_pos; /* foreach position */
uint32_t fe_iter_idx; /* foreach iterator index */
} u2;
};
变量类型【type】
/* regular data types */
#define IS_UNDEF 0
#define IS_NULL 1
#define IS_FALSE 2
#define IS_TRUE 3
#define IS_LONG 4
#define IS_DOUBLE 5
#define IS_STRING 6
#define IS_ARRAY 7
#define IS_OBJECT 8
#define IS_RESOURCE 9
#define IS_REFERENCE 10
/* constant expressions */
#define IS_CONSTANT 11
#define IS_CONSTANT_AST 12
/* fake types */
#define _IS_BOOL 13
#define IS_CALLABLE 14
/* internal types */
#define IS_INDIRECT 15
#define IS_PTR 17
true 和 flase 没有zend_value 结构, 直接通过type来区分,zend_long和double的变量指直接存储在_zend_value中,不需要额外的value指针。
关于php变量属性值的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php变量属性值 php变量的定义的详细内容...