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