Class question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Class question

Post by bokehman »

Code: Select all

class MyClass
{
	var $MyArray = array(1,2,3);
	
	function MyMethod()
	{
		$this->IsBasic = 'like me!';
	}
}
If I want to store $MyArray in a file different from the one that holds the class in your opinion what is the best way to load it?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

It really depends. You'll need to tell more.
For now, check out file() and parse_ini_file() :wink:
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Oren wrote:It really depends. You'll need to tell more.
For now, check out file() and parse_ini_file() :wink:
I know how to open a file. I am asking specifically what would be the best way to do this in class context so I can store the variables used by the class in a seperate file.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Inject it manually:

Code: Select all

class MyClass
{
        var $MyArray = array();
       
        function setArray($array)
        {
                $this->MyArray = (array)$array;
        }
}

require_once "/path/to/array/file.php"

$obj = new MyClass();
$obj->setArray($array_in_file);
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Are you trying to write the object's properties to a file (serialize)? Or are you trying to pass one object's properties to another?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

d11wtq wrote:Inject it manually
If I do it like that I will end up with two copies of the variable, one in the object and one in global scope. I don't want that, the array is too big.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

pass it by reference then ;o
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

bokehman wrote:
d11wtq wrote:Inject it manually
If I do it like that I will end up with two copies of the variable, one in the object and one in global scope. I don't want that, the array is too big.
Not strictly true.

PHP will not "copy" anything in memory until you actually make changes to the value. Copy-on-write.

Passing by reference will help if you are writing to the array though.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

A class definition is required to be in one (and only one) file. So as already mentionned, you'll have to inject it... Personally i'd choose to do that in the constructor...

Code: Select all

public class Foo {
 private $foo;
 public Foo() {
  $this->foo = file_get_contents('whatever.php');
 }
}
Post Reply