V
V
Vatrush2019-03-05 19:06:16
PHP
Vatrush, 2019-03-05 19:06:16

How to get rid of variables when calling methods?

Here is my class (yes bike and all your other terms):

class Picture
{
    public $size = 500000;
    public $type = array("image/jpg","image/png","image/jpeg");

    public function getPic($file){
        if($file['image']['size'] <= $this -> size){
            return $file;
        }
        else {
            return 'Размер изображения превышен';
        }
    }
    public function getType($file){
        if(in_array($file['image']['type'],$this -> type)){
            return $file;
        }
        else {
            return 'Неверный тип изображения';
        }
    }
}

This is how I call the methods:
$image = new Picture();
$getPic = $image -> getPic($_FILES);
$type = $image -> getType($getPic);
$name = 'img';
var_dump( $image -> setName($type, $name));

Everything I call is complete nonsense, because it will be all in variables and passed from one method to another, and so on.
Do I need to set $_FILE class properties? Apparently not, because the IDE scolds me. Tell me how to do it right.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ipokos, 2019-03-05
@Vatrush

what's the problem with passing variables to a method?
set them in the constructor.

class MyClass{

protected $imgObj;

    public function __construct(){
        $this->imgObj = new Image();
    }

    public function otherFunction(){
        $name = $this->imgObj->getName();
    }
}

But this is a bad way, because. in this way, the binding is made hard and it may be necessary to re-configure the object that came.
A better option would be:
public function __construct(Image $image){
        $this->imgObj = $image;
    }

the desired object is already in the class

F
fStrange, 2019-03-05
@fStrange

Store in $file class properties

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question