0

this is my php project dir structure

My_Project
  ->cache
  ->config
  ->lib
 TestClass.php

my TestClass.php contains method cacheSet(), it creates file within the cache/ dir. so my question is how I set path to CACHE_DIR within that method.

class TestClass{

.....

public function cacheSet($filename, $data)
{

    $filename = CACHE_DIR . '/' . $filename. '.cache';
    $file = fopen($filename, 'w');
    fwrite($file, serialize($data));
    fclose($file);
}

}
1
  • and what is your question? Commented Feb 1, 2014 at 6:36

2 Answers 2

1

If you are using a class, you would most likely do this:

public function cacheSet($filename, $data){
    $this->filename = CACHE_DIR . '/' . $filename. '.cache';
    $this->file = fopen($this->filename, 'w');
    fwrite($this->file, serialize($data));
    fclose($this->file);
}

I am not 100% sure of this, but if it's in a class, then it would most likely be this. Then at the start of your class, before you do the class TestClass{ you would want to do something like $class = new TestClass();

Sign up to request clarification or add additional context in comments.

Comments

0

You can use:

class TestClass{

.....
const CACHE_DIR = "your dir";

public function cacheSet($filename, $data)
{

    $filename = CACHE_DIR . '/' . $filename. '.cache';
    $file = fopen($filename, 'w');
    fwrite($file, serialize($data));
    fclose($file);
}

}

So you use const

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.