1

I'm learning namespace in php.

I have created 3 files in vendor/anuj-tbe/social-upload directory and there tree view is like

social-upload
 |- SocialUpload.php
 |- Facebook
       |- FacebookUpload.php
 |- Youtube
       |- YoutubeUpload.php

and their contents are:

SocialUpload.php

<?php
namespace SocialUpload;

class SocialUpload {

}

Facebook/FacebookUpload.php

<?php
namespace SocialUpload\FacebookUpload;

use SocialUpload\SocialUpload;

class FacebookUpload extends SocialUpload {
    private $access_token = '';

    function __construct($access_token)
    {
        $this->access_token = $access_token;
    }

    public function upload($video_file, $file_path)
    {
        return true;
    }
}

Youtube/YoutubeUpload.php

<?php
namespace SocialUpload\YoutubeUpload;

use \SocialUpload\SocialUpload;

class YoutubeUpload extends SocialUpload {
    private $access_token;

    function __construct($access_token)
    {
        $this->access_token = $access_token;
    }

    public function upload($video_file, $file_path)
    {
        return true;
    }
}

Now, I want to use it in my application wherever is required.

Can I access upload() of FacebookUpload or YoutubeUpload class using object of SocialUpload class?

Also, How to add it to composer autoload to access these classes from anywhere using namespace?

1
  • 1
    If you are learning, take a look at php-fig.org/psr/psr-4 standard to simplify composer autoloading. Commented Jun 27, 2017 at 11:47

3 Answers 3

1

Can I access upload() of FacebookUpload or YoutubeUpload class using object of SocialUpload class?

No. Simply put, a SocialUpload is not a FacebookUpload or a YoutubeUpload. If you have a SocialUpload object on one hand (And you know it's of a type youtube/facebook, you could cast and convert the object back to it's original type.) Otherwise, you'd have to create a new object, with information from your original object to create a FacebookUpload or a YoutubeUpload. A FacebookUpload on the other hand, is a type of SocialUpload. You can convert from one to the other, and access public methods on both classes.

Sign up to request clarification or add additional context in comments.

Comments

0

Can I access upload() of FacebookUpload or YoutubeUpload class using object of SocialUpload class?

Your SocialUpload does not know about method upload() of child classes. A solution would be to make SocialUpload abstract with an abstract method upload, that is implemented in concrete Facebook/YoutubeUpload classes.

FYI, this question's answer has a nice example: PHP: How to call function of a child class from parent class

Comments

0

For the first question: why should you do this? My suggestion would be to consider SocialUpload an abstract class and the other two, FacebookUpload and YoutubeUpload, specific classes to work with. In a correct use case you should not instantiate a SocialUpload, but work instead with specific elements. It's like the Animal and Dog classes from all the PHP programming tutorials.

For the second question you should put into your composer.json file the following lines:

"autoload": {
    "psr-4": {
      "YOUR_NAMESPACE": "SOURCE_FOLDER/"
    }
  },

3 Comments

My first question was for 2nd question. I wanted to just use one namespace and call all classes within it. May be I was wrong. Can can I group different classes under same namespace. so that I require to include only one use namespace statement and create object of any class under it like $obj = new FacebookUpload() or $obj = new \SocialUpload\FacebookUpload()
Ok, this means that these lines for the composer should do the job
yes, because there may be more similar class files with similar functionalities added in future and I don't want to add additional use statement for all files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.