Jump to content

parent::__contstruct not working


code333

Recommended Posts

Hello I'm connection to Amazon S3 using a class called S3.php I'm calling a parent constructor but I'm getting null as an object. It only works when I create an object using the new keyword even though I'm extending the S3 class  Below is the segment of the code. Thanks in advance for the help

 

if (!class_exists('S3')) require_once 'S3.php';

class Files extends S3{

	    public $bucket_contents;
		public $fileType;
		public $extraExtension;			

	function __construct($bucket_contents, $fileType, $extraExtension=null){

    //this returns null fails
            $s3 = parent::__construct($accessVar, $keyVar); //S3.php initializes with a __construct
           //this is successful
           $s3 = new S3($accessVar, $keyVar);


 

 

Link to comment
Share on other sites

This is the way subclassing is done, think "Files is a certain kind of S3, like a car is a certain type of vehicle":

if (!class_exists('S3')) require_once 'S3.php';

class Files extends S3{

	    public $bucket_contents;
		public $fileType;
		public $extraExtension;			

	function __construct($bucket_contents, $fileType, $extraExtension=null){
           parent::__construct($bucket_contents, $fileType);
                }
}

//Outside of class
...
$files = new files($accessVar, $keyVar);
echo $files->someMethodOfS3Class();

 

You might be looking for composition, think "Files contains S3,  like car contains engine":

 

if (!class_exists('S3')) require_once 'S3.php';

class Files{

	    public $bucket_contents;
		public $fileType;
		public $extraExtension;			
                        public $s3; //or private?

	function __construct($bucket_contents, $fileType, $extraExtension=null){
                   $this->bucket_contents = $bucket_contents;
                   $this->fileType = $fileType;
                   $this->extraExtension = $extraExtension;
           $s3 = new S3($bucket_contents, $fileType);
                }
}

 

If you want more help from me, please show more of your code, and explain the algorithm you are trying to implement.

Link to comment
Share on other sites

Hello and thank you for your response. Below is the entire code except the S3.php class. Let me know if you need that too

 

 

 


if (!class_exists('S3')) require_once 'S3.php';

class Files extends S3{

	    public $bucket_contents;
		public $fileType;
		public $extraExtension;


	function __construct($accessVar, $keyVar, $bucket, $fileType, $extraExtension=null){

             
		$s3 = new S3($accessVar, $keyVar);

                       //this does not work:
                       // $s3 = parent::__construct($accessVar, $keyVar);

		 $bucket_contents = $s3->getBucket($bucket); 

		$this->bucket_contents=$bucket_contents;
		$this->fileType=$fileType;
		$this->extraExtension=$extraExtension;


		}



function listFiles(){

 foreach ($this->bucket_contents as $file){  
   
      $fname = $file['name'];  
  
          //gets the files with the extension we want
  if ( empty($this->extraExtension) ){
  $ExtensionSplit = ".";
  }else{
  $searchResult = stripos($fname, $this->extraExtension);
  if($searchResult){
   $ExtensionSplit = $this->extraExtension . ".";
  }
  }
  
  if (  end( explode( $ExtensionSplit, $fname ) ) == $this->fileType  ){ 
   
       $furl = "http://s3.amazonaws.com/gmt.dvd.052110/".$fname;  
     
       
       echo "<a href=\"$furl\">$fname</a><br />";  
   }  

	}

}



 function __destruct(){

	$this->bucket_contents;
	$this->fileType;
	$this->extraExtension;

	 }


	}


//$accessVar and $keyVar is populate with actual access and keys
        //this lists the files
$files = new Files($accessVar, $keyVar, "gmt.dvd.052110", "zip", $searchExtraExtension = "mov" );
$files->listFiles();

Link to comment
Share on other sites

Ya, you are definitely not doing inheritance, and not even composition. You are just using the S3 class as a library for now, think "Files class uses functionality provided by the S3 package". You might move into composition as your code evolves, but you are not likely to get into inheritance with this.

<?
if (!class_exists('S3')) require_once 'S3.php';
class Files{
    public $bucket_contents;
    public $fileType;
    public $extraExtension;
    function __construct($accessVar, $keyVar, $bucket, $fileType, $extraExtension=null){
      $s3 = new S3($accessVar, $keyVar);
      $bucket_contents = $s3->getBucket($bucket); 
      $this->bucket_contents=$bucket_contents;
      $this->fileType=$fileType;
      $this->extraExtension=$extraExtension;
    }
    function listFiles(){
      foreach ($this->bucket_contents as $file){  
   
        $fname = $file['name'];  
        //gets the files with the extension we want
        if ( empty($this->extraExtension) ){
          $ExtensionSplit = ".";
        }else{
          $searchResult = stripos($fname, $this->extraExtension);
          if($searchResult){
            $ExtensionSplit = $this->extraExtension . ".";
          }
        }
        if (  end( explode( $ExtensionSplit, $fname ) ) == $this->fileType  ){ 
          $furl = "http://s3.amazonaws.com/gmt.dvd.052110/".$fname;  
          echo "<a href=\"$furl\">$fname</a><br />";  
        }  
      }
    }
}

//$accessVar and $keyVar is populate with actual access and keys
//this lists the files
$files = new Files($accessVar, $keyVar, "gmt.dvd.052110", "zip", $searchExtraExtension = "mov" );
$files->listFiles();

 

What's your programming background, if you don't mind me asking?

Link to comment
Share on other sites

I written a lot of php code from small scripts and a few entire e-commerce sites but more in procedural and want to start mastering OOP. I have a question. By calling the parent constructor aren't I inheriting its constructor's functionality? S3.php constructor initializes the access keys so this is what I want to do with File class and then use S3.php objects/methods. Where is it that I'm going wrong?

Link to comment
Share on other sites

I written a lot of php code from small scripts and a few entire e-commerce sites but more in procedural and want to start mastering OOP.

Cool, ya good luck. It is a fun way to think of code.

I have a question. By calling the parent constructor aren't I inheriting its constructor's functionality? S3.php constructor initializes the access keys so this is what I want to do with File class and then use S3.php objects/methods. Where is it that I'm going wrong?

 

You inherit the functionality by "extending" a class: class Files extends S3. But you don't want to inherit its functionality, you just want to use its functionality. That's different.

 

A Dog "extends" (is an) Animal.

A Person uses (also hopefully is not) a Tool.

 

Hah, that joke totally crept in there.

Link to comment
Share on other sites

I think I'm catching on but still hard to understand. I apologize in advance if I'm asking too much but how would I then code this if I wanted to use inheritance and not just as a library. I'm pretty stuck on how this would come about.

Link to comment
Share on other sites

Sure.

<?
if (!class_exists('S3')) require_once 'S3.php';
class Files extends S3{
    public $bucket_contents;
    public $fileType;
    public $extraExtension;
    function __construct($accessVar, $keyVar, $bucket, $fileType, $extraExtension=null){
      parent::__construct($accessVar, $keyVar);
      $bucket_contents = $this->getBucket($bucket); 
      $this->bucket_contents=$bucket_contents;
      $this->fileType=$fileType;
      $this->extraExtension=$extraExtension;
    }
    function listFiles(){
      foreach ($this->bucket_contents as $file){  
   
        $fname = $file['name'];  
        //gets the files with the extension we want
        if ( empty($this->extraExtension) ){
          $ExtensionSplit = ".";
        }else{
          $searchResult = stripos($fname, $this->extraExtension);
          if($searchResult){
            $ExtensionSplit = $this->extraExtension . ".";
          }
        }
        if (  end( explode( $ExtensionSplit, $fname ) ) == $this->fileType  ){ 
          $furl = "http://s3.amazonaws.com/gmt.dvd.052110/".$fname;  
          echo "<a href=\"$furl\">$fname</a><br />";  
        }  
      }
    }
}

//$accessVar and $keyVar is populate with actual access and keys
//this lists the files
$files = new Files($accessVar, $keyVar, "gmt.dvd.052110", "zip", $searchExtraExtension = "mov" );
$files->listFiles();

 

That's basically it.

Link to comment
Share on other sites

Thanks! so the real difference in my code was the assigning of the constructor. So I was using it like I was calling a function as oppose to treating it like it already existed in code. That's why you used $this as in this current object to access its method. I was pretty close though, yes? So in future reference I have to think of it like I'm programming inside the class? What made you ask me my programming background - just curious. Thanks.

Link to comment
Share on other sites

I asked about your programming background because using a class for the task at hand (Files class) somehow seemed forced, not a great fit. I would have written a function; what's a class with one function?

 

Ya, the difference was you were assigning the value, but not because of the reasons you list there, it is just basically that the constructor does not return a value to be assigned to a variable, like $s3.

Link to comment
Share on other sites

I see. The file class will be expanded with more functionality. I just got stuck on that parent section. I'm going to add more retrieval information such as file size, date created, etc.. This should be good enough to create a class, yes? I thank you very much for the insight. Do I give you any kind of credit here?

 

I have another question, if it is okay. I have worked mainly as a contractor for over 5 years for different clients needing off site web masters and occasionally developing web applications (e-commerce, web form applications, etc.). I'm thinking about going into the 9-5 world and work for a company (more security and a constant paycheck). I'm thinking of a mid level web application developer position. I have experience in php/mysql, web server linux administration, some front-end, a little bit of ajax, javascript, photoshop, css, etc.. Do you have any insight or recommendations in what employers expect and what technology I should prepare more in? Thanks.

Link to comment
Share on other sites

You're welcome. Ya, keep working on it.

 

You know what you should probably do to get a feel of what they are looking for? Look for job ads, apply to each one of them, interview, and after a while you will have the answer to your question, and you will know what they are looking for.

 

I've been looking for a while now, but my city is small, so I don't really know what to tell ya. You could post a new thread in one of the forums. But yes, really whatever their ads say is what they want.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.