Jump to content

declaring objects in class body


davestewart

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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'

)

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();

?>

Link to comment
Share on other sites

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();

?>

Link to comment
Share on other sites

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

 

 

 

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.