- This topic is empty.
-
AuthorPosts
-
September 16, 2014 at 11:27 pm #183339
chrisburton
ParticipantOk. So I just jumped in and tried an example and I can’t seem to get an output. I feel like it’s something quite miniscule but not sure what exactly. Is it my syntax or arguments?
<?php class animal { function name ($type='') { return $this->name = $type; } } $animal = new animal('dog'); var_dump( $animal->name() ); //Output: string(0) "" ?>
September 16, 2014 at 11:41 pm #183340Ilan Firsov
ParticipantFirst thing you don’t have a constructor that takes arguments, so the line
$animal = new animal('dog');
does nothing with the argument you providedSecond thing is that when you call the
name
function without arguments it defaults to name=” which is then returned.
I’d do something like this:function name ($type) { if(!empty($type)) { $this->name = $type; return $this->name; } else { return $this->name; } }
September 16, 2014 at 11:46 pm #183341chrisburton
ParticipantFirst thing you don’t have a constructor that takes arguments, so the line $animal = new animal(‘dog’); does nothing with the argument you provided
Ah
__construct
is what is required when usingnew
.<?php class animal { public function __construct($name) { $this->name = $name; } public function name() { return $this->name; } } $animal = new animal('dog'); var_dump($animal->name()); //Output: string(3) "dog" ?>
I just figured I could do this with only one function.
September 17, 2014 at 4:29 am #183360Alen
ParticipantI just figured I could do this with only one function.
Well there is only one method (function). The constructor is responsible for constructing the object. It initializes the values you pass. The getter method is just that, a way to get the value.
class Animal { # Property public $name; # Constructor function __construct($name) { $this->name = $name; } # Getter public function getName() { return $this->name; } } $animal = new Animal('Dog'); echo $animal->getName();
September 17, 2014 at 12:08 pm #183399chrisburton
ParticipantThanks @AlenAbdula. I see you added the
public $name
property. I see that here and there within tutorials but I also see it omitted. When should I set this and when can I leave it out?September 17, 2014 at 12:21 pm #183401__
ParticipantI see that here and there within tutorials but I also see it omitted. When should I set this and when can I leave it out?
As a rule, you should always define the properties your class uses. This makes it easier to plan/organize your code, and lets you recall at a glance what it does.
This also goes back to your public/private question (though if you’re still concentrating on other things feel free to ignore this part for now; read it when you’re ready):
If you just assign a property (like
$this->name = $name;
in your constructor), it will be “public.” There’s not much effective difference in declaring the property beforehand, in the class definition, but it does make plain that the property exists and can be relied upon.“Public” means that the property can be accessed/assigned/changed from anywhere. So, this sort of thing can happen:
$dog = new animal( 'dog' ); $dog->name = 'raccoon'; $dog->name(); // prints "raccoon"
If you want to make sure this cannot happen, you can make the property
private
:class animal { private $name; public function __construct($name) { $this->name = $name; } public function name() { return $this->name; } }
Then:
$dog = new animal( 'dog' ); $dog->name = "raccoon"; // fatal error: Cannot access private property animal::$name … $dog->name(); // prints 'dog'
September 17, 2014 at 12:45 pm #183408chrisburton
Participant@traq The only part that is confusing is that you’re using
getName
, where is that coming from since you never specified that function?September 17, 2014 at 1:06 pm #183410__
ParticipantSorry, I used the method name from Alen’s example (but copied the class definition from yours). Fixed.
edit
I usually name “getter” and “setter” methodsget{propertyName}
andset{propertyName}
, just to make their purpose obvious.Also, if you decide to use private (or protected) properties or methods, it is a very common convention to start their name with an underscore (i.e.,
$_name
instead of$name
). Again, it just keeps things easy to read and understand.September 17, 2014 at 1:13 pm #183411chrisburton
ParticipantThought I was losing my mind for a second. Now that makes much more sense to me. I’m going to try to find other examples I can use to practice. Thanks, guys.
September 17, 2014 at 1:15 pm #183412chrisburton
ParticipantAlso, if you decide to use private (or protected) properties or methods, it is a very common convention to start their name with an underscore (i.e., $_name instead of $name). Again, it just keeps things easy to read and understand.
Ah, yes. Saw this too in tutorials. Thanks for that.
September 17, 2014 at 11:31 pm #183445chrisburton
ParticipantJust leaving this here as it’s incredibly well written and helps explain public, private and protected methods.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.