Ramesh Pareek
Ramesh Pareek

Reputation: 1669

Calling method from another class in php

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() )?

Upvotes: 1

Views: 9027

Answers (1)

Sohel Ahmed Mesaniya
Sohel Ahmed Mesaniya

Reputation: 3450

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

Upvotes: 4

Related Questions