Jump to content

Learning Classes.


fraginhell

Recommended Posts

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]
<?php
class 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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.