1

Hey I have been reading some PHP class documentation and how to create classes. But I haven't seen any examples of properties inside the properties. I will give an example what I want to do:

class Properties {
public property1;
public nestedProperty1;
public nestedProperty2;
}

and I want to assign properties like this:

$Property = new Properties();
$Property->property1 = "foo";
$property1->nestedProperty1 = "bar";

So I could access data like:

$property1->nestedProperty1;

Is this possible? I need this because I'm working with a dynamic multidimentional array.

1
  • 2
    First one, you must have $ at beginning of your variable - public $property1; Second, if you want something like $Property->property1->nestedProperty1, you can assign $Property->property1 = new Properties(); Commented Aug 27, 2018 at 14:34

1 Answer 1

1

1) classes != arrays.

2) You want nested CLASSES.

For example (pseudo code):

// Create a class
MyThingClass {
  string ThingProperty1
  int ThingProperty2
}

// Create a MyThingClass instance
MyThing = new MyThingClass(...)

// Use instance in another class
MyOtherThingClass {
  MyThingClass MyThing
  string SomeOtherProperty
}

You cannot nest properties directly like you are trying to do. You could also use structures as properties.

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

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.