1
votes

I am reading this documentation:

http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html

This is an example model suggested in the guide,

namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $imageFile;

    public function rules()
    {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
        ];
    }

    public function upload()
    {
        if ($this->validate()) {
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        } else {
            return false;
        }
    }
}

What I don't understand is how is the saveAs() method being called in upload() function above:

 $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);

According to whatever little php I know, methods are called either statically, like this:

UploadedFile::saveAs(..., ...);

Or non-statically like this:

$this->saveAs();

but in the latter case, must not the class from which the method is called, extend from the class which the method belongs to?

The saveAs() function belongs to the yii\web\Uploadedfile class. how can we call it the above mentioned way ( $this->imageFile->saveAs() )?

1
rules() tells the system to store an upload object in $this->imageFile, basically. once the upload has been processed interally, you simply have an object in there and can call its methods. - Marc B
my question is just about simple working of php. how can we call a method from another class (yii\web\UploadedFile) in this model class '$this->saveAs()' ? especially when this file is not extending from yii\web\UploadedFile! - Ramesh Pareek
there is no $this->saveAs(). there's $this->imageFile->saveAs(), because that uploadedfile object was placed into your $this->imageFile when the rules were processed. - Marc B
can you kindly elaborate it in your answer? i've read the rules() and the vaildate() methods in yii\base\Model, but don't understand how is uploadedfile object being placed in the imageFile property. - Ramesh Pareek
this is the line you are looking for : $model->imageFile = UploadedFile::getInstance($model, 'imageFile') just before calling $model->upload() in the controller (here). $this->imageFile will hold then an instance with a saveAs() method. - Salem Ouerdani

1 Answers

4
votes

Consider following example of Basic OOP concept in PHP

There are 2 files in a web accessible directory like in htdocs if using XAMPP

One.php

<?php
// One.php

/**
 *
 */
class One
{
    public $one_p;
    // function __construct(argument)
    // {
    //     # code...
    // }

    public function test_two()
    {
        var_dump($this->one_p);
    }
}

Two.php

<?php
// Two.php
require 'One.php';
/**
 *
 */
class Two
{

    // function __construct(argument)
    // {
    //     # code...
    // }

    public function test_one()
    {
        $one_obj = new One;
        $one_obj->one_p = 'hello prop';
        $one_obj->test_two();
    }
}

$two_obj = new Two;
$two_obj->test_one();

Now run Two.php in browser and observe the result, it is string(10) "hello prop"

Now comment $one_obj->one_p = 'hello prop'; line and observe result, it is NULL

So we can conclude that once a property (a variable) is set, it is globally accessible. This is the concept of getters and setters in PHP OOP. Please refer here. You don't need to pass it in argument like you need in function

In Yii example

$one_obj->one_p = 'hello prop';

is like below

$one_obj->one_p = new Someclass;  // here Someclass should be 'require' at the begining of file

so here you can access all properties and method of Someclass like $one_obj->one_p->somemethod();

Since getInstance() is static method of UploadFile, you can call it without creating object.

And getInstance() returns an object You can store anything you need to store in one_p like int, float, array, resource, object ... Hope you got it.

Yii is a very fine framework of PHP completely coded in OOP style, not coded in procedural style, engaging MVC architecture. You will enjoy it more, just go through here