1

I have been moving website made in Cakephp into Django. In one place I found get_declared_classes(). I thinks this function returns list of previously used classes before running current class.

First time when I encounter this, I just store list of classes manually in one file and I was using that and this solution worked only for a particular web page but Now I have multiple pages calling this class and everytime list of classnames are very different so I can not store class name list.

This code is actually connecting and fetching data from here and I want to replace this class in python(We are replacing whole website). The only problem I have is how to replace get_declared_classes.

class HrbcConnect{

    private $scope = '';
    private $token = null;
    private $token_expire = 0;
    private $request_opt = array();
    public $id;
    public $id_arr = array();
    public $write_error;

   public function read($resource,$condition='',$field=array(),$order=null){
   $declared_classes = get_declared_classes();
   foreach(App::objects('Model') as $v){
      if(in_array($v,$declared_classes)){
          $instance = new $v;
          if(property_exists($instance,'hrbc_cols') && array_key_exists(ucfirst($resource),$instance->hrbc_cols)){
               foreach($instance->hrbc_cols[ucfirst($resource)] as $vv){
                    if(is_array($vv)){
                        $field[] = $vv[0];
                    }else{
                        $field[] = $vv;
                    }
                }
            }elseif(property_exists($instance,'hrbc_cols_arr')){
                foreach($instance->hrbc_cols_arr as $kk=>$vv){
                    if(array_key_exists(ucfirst($resource),$vv)){
                        foreach($vv[ucfirst($resource)] as $vvv){
                            if(is_array($vvv) && !in_array($vvv[0],$field)){
                                $field[] = $vvv[0];
                            }elseif(!is_array($vvv) && !in_array($vvv,$field)){
                                $field[] = $vvv;
                                    }
                                }
                            }
                        }
                    }
                }
            }
   }


}

When I print the $v in the above code to find what are classes being used, I found list of classes that defined in my models.

If question is not clear please let me know, I can provide more information.

Is there any other library that can replace this function in Python? Or Is there any other solution I can try ?

00noob
  • 152
  • 1
  • 10
  • The very short answer is that you don’t replace this function. I can pretty much guarantee that it was used and directly followed by (or directly consumed by) some code that did something unique, and hopefully commented, but also hopefully obvious. Or, something old. Please post the code that used it, and we can tell you more. – Chris Haas Jul 05 '22 at 04:01
  • Thank you so much for your time, I have added code that is using that function. – 00noob Jul 05 '22 at 04:54
  • 1
    Please check this answer maybe it will cover your case. https://stackoverflow.com/a/1796247/6696072 – Mr.Teapot Jul 05 '22 at 05:22
  • That code is effectively checking if a class exists. – AD7six Jul 05 '22 at 06:31
  • I would caution against this kind of line by line migration… define and migrate functionality (here looks like “parse the api response”), or generally draw a boundary around code and implement the touch points, not the inner details. Otherwise you’re lift-and-shift ing code design, bugs and foibles from one language to another and ultimately with have something that literally looks like php code in python (which will be awful to maintain). – AD7six Jul 05 '22 at 06:48
  • Yes, Thank you, I was able to solve this problem after understanding why get_declared_classes() is being called. Classnames are being used to fetch variable `hrbc_cols` from each class and I designed in such way that I dont need `get_declared_classes()`. Thank you everyone for your valuable suggestion. – 00noob Jul 05 '22 at 09:38

0 Answers0