- Edited
new classname is it and object ?
assing the new object to a variable $a.
$a is a variable but what is it ->attribute
class classname{
public $attribute;
}
$a = new classname();
$a->attribute = "value";
echo $a->attribute;
new classname is it and object ?
assing the new object to a variable $a.
$a is a variable but what is it ->attribute
class classname{
public $attribute;
}
$a = new classname();
$a->attribute = "value";
echo $a->attribute;
bertrc new classname is it and object ?
Technically, you have created an instance of a class. Yes it is an object, with properties (class variables) and methods (class functions).
In the class definition, $attribute is the name of a (public) property (class variable.) You are setting the property (class variable) using $a->attribute = "value"; and you are referencing the property (class variable) using echo $a->attribute;
This might make it a bit more obvious (or not?)
class classname{
public $message = 'Hello, World!';
private $nope = "Can't touch this";
}
$foo = new classname();
echo $foo->message . "\n";
$foo->message = 'Goodbye, cruel world.';
echo $foo->message . "\n";
echo $foo->nope;
Result:
Hello, World!
Goodbye, cruel world.
PHP Warning: Uncaught Error: Cannot access private property classname::$nope . . .
For the official documentation on this, Classes and Objects. classname
is the name of a class, attribute
is a property belonging to the class. $a
is an object that is an instance of classname
. You then set its attribute
property to have the value "value"
. And, for the record, marking the property public
makes it visible outside the class so that you can set and read its value.