Mhm, $resource is a property, not a method.
Your code has the general idea, but a few things aren’t quite right. I know it’s just an example, so we can’t be too specific, but here’s a few things just for general reference:
First of all, all variables in PHP have a dollar sign for the first character. So instead of someObject, someValue, attributes, etc., you’d have $someObject, $someValue, $attributes, and so on. When you’re accessing a property, you drop the dollar sign (e.g. $modx->resource).
Also, when you’re creating a new object, use parentheses as if you were calling a function. When you create an instance of a class (in other words, "new object()"), you’re actually also calling the constructor (lookup the __construct() function of classes), which accepts parameters. So you could have: "$someObject = new someClass(’some value for a parameter in the constructor’)"
// You had this:
someObject = new otherClass;
// Should be this:
$someObject = new otherClass();
$someValue = $modx->resource->get('someAttribute');
Now, variables declared outside a class are not accessible inside the class. Your variable declaration for $resource is trying to access the $someObject:
public $resource = object someObject; // should be $someObject, by the way, and the word "object" is not necessary
The problem is that you declared $someObject outside the class, and the class has no idea it exists. Think of classes as completely separate from the rest of your code. Classes are like templates. When you want to create an object, you create an instance of a class, which is like using the class as a template for your object. So no variables outside of a class can be access from inside a class because the class itself doesn’t know anything about the rest of your code.
I think this code might be a better of example to explain what’s going on in your original question:
<?php
class modx {
public $resource = new modxResource(); // that's not really what the resource class is called, but I don't know exactly what it is
}
class modxResource {
public function get($attribute) {
// get the attribute and return the value
}
}
$modx = new modx();
$someValue = $modx->resource->get('someAttribute');
?>
So what’s going on here is we have a variable called $modx that contains a modx object (the modx object is an instance of the modx class). The modx object itself has a property called $resource. $resource contains a resource object (which is an instance of the modxResource class). The get() method is part of the modxResource class.
When you do "$modx->resource->get(’someAttribute’)", you’re calling the get() method on the resource object, which is stored as a property of the modx object.
Let’s say you had just this:
$resource = new modxResource();
$someValue = $resource->get('someAttribute');
Here you have a resource object, and you’re called its get() method. Now think about $resource being inside of the modx object. It becomes the $resource property, and everything is still exactly the same, except that now when you want to access it, you have to go through the modx object because the resource object is stored inside the modx object.
One more thing that might clarify some of this. I don’t know how much PHP you know, so I don’t mean to assume you don’t know what you’re doing, but it’s just easier to be thorough.
In PHP, a variable (which always has that $ in the name) can store any type of value, including arrays and objects.
$someString = 'This is a string.';
$someNumber = 1234;
$someArray = array('dog', 'cat');
$someObject = new someClass();
$someObject contains an object, just like $someString contains a string.
Properties of classes are still variables, and methods of classes are still functions. Since access a property with the arrow (and without the dollar sign), when you do something like "$modx->resource", you’re getting the object that’s stored in the $resource property. When you do "$resource->get()", you’re calling the get() method of the resource object stored in $resource. You put that all together into one line to get "$modx->resource->get()"
I think I’m getting too wordy now and it might be confusing, so does that make sense?