code333 Posted June 3, 2010 Share Posted June 3, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/ Share on other sites More sharing options...
dabaR Posted June 3, 2010 Share Posted June 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067396 Share on other sites More sharing options...
code333 Posted June 3, 2010 Author Share Posted June 3, 2010 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(); Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067423 Share on other sites More sharing options...
dabaR Posted June 3, 2010 Share Posted June 3, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067439 Share on other sites More sharing options...
code333 Posted June 4, 2010 Author Share Posted June 4, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067443 Share on other sites More sharing options...
code333 Posted June 4, 2010 Author Share Posted June 4, 2010 I've added a link to the S3.php class in case you want to take a look at it. I appreciate the help, thanks. http://guitar-music-theory.com/DVD_downloads_452010_52410/s3-php5-curl/S3.zip Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067447 Share on other sites More sharing options...
dabaR Posted June 4, 2010 Share Posted June 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067451 Share on other sites More sharing options...
code333 Posted June 4, 2010 Author Share Posted June 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067455 Share on other sites More sharing options...
dabaR Posted June 4, 2010 Share Posted June 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067458 Share on other sites More sharing options...
code333 Posted June 4, 2010 Author Share Posted June 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067466 Share on other sites More sharing options...
dabaR Posted June 4, 2010 Share Posted June 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067468 Share on other sites More sharing options...
code333 Posted June 4, 2010 Author Share Posted June 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067475 Share on other sites More sharing options...
dabaR Posted June 4, 2010 Share Posted June 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067503 Share on other sites More sharing options...
code333 Posted June 4, 2010 Author Share Posted June 4, 2010 Thanks again dabaR Quote Link to comment https://forums.phpfreaks.com/topic/203799-parent__contstruct-not-working/#findComment-1067746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.