I am trying to learn php oop and am just wondering if person1 = new person related to the class Person? Can I call it anyname such as person1 = new dad? etc
<?php
class Person{
public $name;
public $email;
}
$person1 = new person;
I have been following a tutorial on youtube and I got the following errors:
Fatal error: Uncaught Error: Call to undefined method Person::__contruct() in C:\xampp\htdocs\crash course\person.php:43 Stack trace: #0 C:\xampp\htdocs\crash course\person.php(57): Customer->__construct(âMervin Leeâ, âexampl@hotmaâŚâ, 300) #1 {main} thrown in C:\xampp\htdocs\crash course\person.php on line 43
But my codes below look correctâŚ
<?php
class Person{
private $name;
private $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email =$email;
echo __class__.'Created<br>';
}
public function __destruct() {
echo __class__.'Created<br>';
}
public function setName($name){
$this->name = $name;
}
public function setEmail($email){
$this->email = $email.'<br>';
}
public function getEmail(){
return $this->email;
}
public function getName(){
return $this->name.'<br>';
}
}
//$person1 = new person('Merin Lee', 'example@example.com');
// echo $person1->getName();
// echo $person1->getEmail();
class Customer extends Person{
private $balance;
public function __construct($name, $email, $balance) {
parent::__contruct($name, $email, $balance);
$this->balance = $balance;
echo 'A new '.__class__.'has been created';
}
public function setBalance($balance){
$this->balance = $balance;
}
public function getBalance(){
return $this->balance.'<br>';
}
}
$customer1 = new Customer('Mervin Lee', 'example@example.com', 300);
echo $customer1->getBalance();
I was following a tutorial on youtube and the person has 3 parameters for the parents. I tried it after fixing my typo and it does work but i was wondering as well⌠should it have 2 parameters?
From my understanding, you are just passing an additional property $balance which should be fine
Yes, but youâre passing a third parameter to a function that is defined to accept only two.
If fixing your typo has fixed the issue, then maybe itâs OK, Iâm just surprised. I had a quick search but the only example I could find had the same number of parameters in the definition and the calling line.
Itâs correct. Heâs going back and throwing those variables in the parentâs constructor. However, this seems redundant because the parent class already should be passing those parameters into the child class. You just need to extends the parent class.
Thatâs why I thought it was incorrect - the parentâs constructor is defined to accept two parameters, yet heâs sending three.
Iâve done very little with OOP, I find I canât learn stuff unless I can think of a job to do with it and that has eluded me so far. Iâll perhaps pick up on that if/when I start to try it out.
I have seen code and in fact have written some myself that accepts different numbers of arguments. That is, similar to this:
function somefunction($foo, $bar, $baz = true) {
It can then be called passing three
somefunction('bou', 'yah', false);
or two
somefunction('cri', 'key');
And I have seen code that accepts an argument array and does different things depending on the number of arguments in the array. But AFAIK, the function / class needs to be written to work that way, and there is no automatic âignore extra arguments without throwing an errorâ.
As I understand it, you have to declare defaults for optional parameters like this ($baz = true). So they will be defined whether you add them or not in the function call.
Same like other languages do. Why should there be an issue from passing more arguments than parameters? The only thing you lose is the convenience of having variables already assigned and type hinting.
You wonât get an error for supplying more than the expected number. I have always assumed that PHP just ignores these arguments until the functions have the exact amount of parameters. Then itâll start complaining if you donât supply the correct amount. So in essence, the function is what really dictates what is really needed. Supplying with random variables afterwards will be âignoredâ if you will.