Instantiating an object

Hey guys!

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;

It should be $person1 = new Person;.

Person is the name of the class. $person1 is the name of the Person object you are instantiating.

If you have $person1 = new Dad, you must have a class called Dad.

1 Like

Also, you are setting properties in case you didn’t know. So to use them, you have to do

$person1->name = 'John Doe';
$person1->name ='Jane Doe';

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();

it’s a typo.

1 Like

Right here. It should be __construct.

Your class starts with a capital P, but you tried to instantiate with lower-case.

1 Like

I don’t know much about OOP, something I’d like to learn, but is this bit correct?

parent::__contruct($name, $email, $balance);

That is, you’re calling the __construct method in your parent object, but that is defined as

 public function __construct($name, $email) {

with only two parameters. Won’t that cause a problem, or have I misunderstood what that line of code is doing?

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”.

2 Likes

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.

3 Likes

Well that’s what I thought. But the OP just defines it for two parameters, then calls it with three, but it works.

Not something I have ever tried. You would expect at least a warning.

That would be inconvenient and then it wouldn’t make sense to have a function like func_get_args().

There are several built-in functions that accept more parameters than defined (e.g. array_merge(), array_diff(), etc. ).

1 Like

So php just ignores any superfluous params on functions not designed to take an indefinite number?

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.

2 Likes

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.