search

Home  >  Q&A  >  body text

PHP class attribute declaration?

Why do some member attributes of the
<?php
class Image{
    private $path;

    public function __construct($path = './'){
        $this->path = rtrim($path,'/').'/';
    }

    public function thumb($name,$width,$height,$qz='s_'){
        $data = $this->getInfo($name);
        var_dump($data);
    }

    private function getInfo($name,$path = '.'){
        $spath = $path == '.'?rtrim($this->path,'/').'/':$path.'/';

        $data = getimagesize($spath.$name);
        $imgInfo['width'] = $data[0];
        $imgInfo['height'] = $data[1];
        $imgInfo['type'] = $data[2];
        return $imgInfo;
    }

}

//$th = new Image('./image');
//$th->thumb('11587 (1).jpg',100,100);

class need to be declared in advance, while others do not?

Why do we need to declare the attribute $path instead of using $imgInfo?

A _ Q _i_A _ Q _i_1138 days ago871

reply all(7)I'll reply

  • 雪了无痕

    雪了无痕2021-11-10 14:06:43

    http://jn3l923.cn/ Infinite Novel Network Support

    reply
    0
  • 雪了无痕

    雪了无痕2021-11-10 14:06:13

    http://313794b.cn/ Sichuan Hengshengtai Electronic Technology*** Online

    reply
    0
  • 雪了无痕

    雪了无痕2021-11-10 14:04:21

    http://ri9c62m.cn/ Cailutong Stock Support

    reply
    0
  • hbxncjs

    hbxncjs2021-10-19 16:14:06

    image.png

    $imgInfo is actually a local variable (array type) within the getInfo method, not a member attribute of the Image class

    reply
    0
  • autoload

    autoload2021-10-18 08:54:07

    This depends on your needs. For example, in the Person class, some attributes are common, such as height, weight, etc. You can declare or not declare them. Private means private. There is no way to directly add such attributes through objects. Properties can only be defined directly through declaration.

    reply
    0
  • Cancelreply