fraginhell Posted November 9, 2006 Share Posted November 9, 2006 Hi Everyone,Please be gentle, i'm just starting to walk down the php path :)I'm trying to get my head round php classes. I've created a very simple class to get the size of a file.I have created a function to get the file size which works fine. However if I want to to this a default action when I create the class I added the function to the constructor, but php moans that it can find the function. My guess is either my logic is wrong or some thing to do with scope?(code below) any advice is welcomed at this point, while I still have hair ;)[code]<?phpclass fileInf{ var $file_name; var $size; function set_size() { filesize($this->file_name); } function get_size() { echo "File size is : " . $this->size; } function fileInf($filename) { $this->file_name=$filename; set_size(); }}$myfile = new fileInf("c:\pagefile.sys");$myfile->get_size();?>[/code]thanks again. Keith Link to comment https://forums.phpfreaks.com/topic/26661-learning-classes/ Share on other sites More sharing options...
Monkeymatt Posted November 9, 2006 Share Posted November 9, 2006 Instead of calling set_size(), you need to call $this->set_size(). You need to let it know that you are calling the function from this case of the class.Also, how are you getting the filesize to $this->size, as set_size() simply calls filesize(), but does not put the value in the variable $size. I think you would need something more like this:[code]function set_size() { $this->size=filesize($this->file_name);}[/code]Monkeymatt Link to comment https://forums.phpfreaks.com/topic/26661-learning-classes/#findComment-121962 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.