修改父类变量

时间:2017-02-04 10:23:26

标签: php oop

假设我在A和B类中都有一个相同的变量。

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        //string valuess = Commanfunction.logintokencheck(user_id, token);
        try
        {                
            //if (valuess == "True")
            //{
                DataTable dt = new DataTable();
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM [nwt].[usermessage] where [to_user] = '" + user_id + "' AND [msg_user_type] ='inbox' AND [reviwe] =1 AND ([status]='active' OR [status]='keep')", sconn))
                {
                    sconn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);

                    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                    Dictionary<string, object> row;
                    string x = "{\"status\":\"200ok\",\"reviewcount\":" + dt.Rows.Count;
                    // string y= serializer.Serialize(x);
                    //foreach (DataRow dr in dt.Rows)
                    //{
                    //    row = new Dictionary<string, object>();
                    //    foreach (DataColumn col in dt.Columns)
                    //    {
                    //        row.Add(col.ColumnName, dr[col]);
                    //    }
                    //    rows.Add(row);
                    //}
                    //string z = serializer.Serialize(rows);
                    return new RawJsonActionResult(x + "}");
                    //return (x + "\"countries\":" + z + "}");

                }

如果两个变量具有相同的名称,如何从子类更改父变量?

我不想使用额外的静态功能。我只是想直接修改它。

1 个答案:

答案 0 :(得分:-1)

您可能正在考虑从子类的对象更改父类对象的属性,这将以这种方式实现

class A {
      public $status;
      public function __construct(){}
      }

class B extends A {
      public $status;
      public function __construct(){}

      public function modifyParentStatus(){
      /* In the next line i want to change the parent class variable 
      But it changes the current class variable */
      //you need to create an instance of the parent class for you to access its properties
      $a = new A();
      $a->status = 'active';
      return $a->status;
     }
} 

$obj = new B();
echo $obj->modifyParentStatus();