davestewart Posted December 10, 2007 Share Posted December 10, 2007 Hi everyone, I want to group my variables in my class declaration so they're easier to manage. Using arrays I could go: class Test{ var $foo = array( 'bar' => 0, 'baz' => 'Hello' ) } But is there a similar way to specify object-access? I'd rather not have to do it all in the constructor with $foo->bar = 0, but just lay it out in the class body. Cheers! Dave Quote Link to comment Share on other sites More sharing options...
tkstock Posted December 11, 2007 Share Posted December 11, 2007 Not sure what you mean by "lay it out in class body" The class contains local variables / objects and functions. Use any combination of these to accomplish what you're looking for. You can use the special __construct function to create a constructor: function __construct() { // initialize variables and objects here } HTH Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 Hi Tom, Thanks for replying. Don't worry - I'm all au-fait with classes. In fact this isn't even an OOP question but it got moved here. I just want to know if there's a shorthand way to create objects, as there is for arrays: // shorthand array creation $foo = array( 'bar' => 0, 'baz' => 'Hello' ) // is there a way to do this with objects? $foo = someObject( bar->0, baz->'Hello' ) Quote Link to comment Share on other sites More sharing options...
tkstock Posted December 11, 2007 Share Posted December 11, 2007 Quite simply: $myobject = new MyObject($param1,$param2,...); The parameters are passed to the constructor, so use the same number of parameters! Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 Hi Tom, Sorry - I don't think you're understanding what I'm asking. Perhaps I'm not being clear enough. Here's the thing: you can make a deep hierarchical list of properties by creating an array, and the resulting object will be accessed using array-access sytnax, ie value = array['key'] In a very deep array, you might access a value like so: value = array['key']['sub-key']['sub-sub-key'] I want to be able to build an object (NOT a class) in teh same way you;d write out an array, then access it the same way. Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 Check this post: http://www.phpfreaks.com/forums/index.php/topic,171610.0.html I've half-cracked the bit about writing variables out, but realised that there's no way I'm going to be able to do this in class variable declarations. var $obj->skills->php = 'good'; ... is NOT going to work! As I said, check that post and see what I'm driving at. Is it possible? Cheers, Dave Quote Link to comment Share on other sites More sharing options...
tkstock Posted December 11, 2007 Share Posted December 11, 2007 I think your terminology is confusing, for the most part. What does an object have that a class doesn't? In my mind, they are synonymous. It seems to me that (as thorpe said in your other post) you're there. What more do you want? Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 Sorry - it's my JavaScript background. I'm just used to building objects on the fly. The difference is you can't use object->access in a class body. The syntax throws an error, so I want to know if it IS possible, or I'm barking up the wrong tree. If it's not, then so be it. Thanks everyone for their time Quote Link to comment Share on other sites More sharing options...
tkstock Posted December 12, 2007 Share Posted December 12, 2007 I guess the question is now more of a "why would you want to" question...? By trying to access a class in the "body" (which I assume you to mean outside of a function or constructor), you are likening it to a script, which classes are not. The class has to have a sense of the object it holds - the object has to be instantiated and held within the class. If you're trying to access the object outside the class, create a function to return it. Here's a quick example: class Test() { private $stest; function __construct() { $stest = new SubTest(); // instantiate the sub class } function get_subtest() { return $this->stest; } } Subsequently, calls to get the SubTest of the Test class would be: $test = new Test(); $subtest = $test->get_subtest(); You could then, theoretically, have subtest creating another subsubtest object within it's constructor, etc. So, when you create the test class, all the sub classes are created automagically! Hope that helps Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 12, 2007 Share Posted December 12, 2007 you can cast a variable/array as an object $var = (object) array('first' => 1, 'second' => 2); Quote Link to comment Share on other sites More sharing options...
trq Posted December 12, 2007 Share Posted December 12, 2007 Even with the casting however, I'm getting a... Parse error: syntax error, unexpected T_OBJECT_CAST in /home/thorpe/scratch/x.php on line 6 error. #!/usr/bin/php <?php class foo { private $var = (object) array('first' => 1, 'second' => 2); public function pr() { print_r($this->var); } } $foo = new foo(); $foo->pr(); ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 12, 2007 Share Posted December 12, 2007 I discovered that things like that [some operations/function calls/whater] do not work when declaring properties for a class this works though <?php class foo { private $var = array('first' => 1, 'second' => 2); public function pr() { $var = (object) $this->var; print_r($var); } } $foo = new foo(); $foo->pr(); ?> Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 13, 2007 Author Share Posted December 13, 2007 Thanks everyone for pitching in. The cast is a great idea (I'm surprised it works, but it does!) however it's not a recursive cast, so sub-arrays are left as arrays. I could write a function to do it for me, but it's all beginning to smell a bit hacky, so I think I'll stop trying to shoehorn a square peg into a round hole. An interesting voyage though! Tom, in order to answer your question as to why: basically I would prefer the syntax of $this->propertyGroup->property over $this->propertyGroup['property'] But it's no deal breaker. Thanks again everyone, Cheers, Dave Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.