Tim C
Tim C

Reputation: 5714

PHP OOP Construct explination unclear

This is not a question about a particular problem, but rather a question asking for advice and / or assistance.

Im a 2nd year student, Im really struggling to get the hang of OOP programming, the fact that my textbook is not very clear, in its explanation is not helping either. I know there are 100's of examples on the web of this probably but I would like to focus specifically on the example used in my textbook.

The introduction to OOP in php starts with this: EXAMPLE 1

class Person{
var $name;
function set_name($data){
    $this->name=$data;
  }

function get_name(){
    return $this->name;
  } 
}
$ralph = new Person;
$ralph->set_name('Ralph');
echo 'friend name is '.$ralph->get_name();

Fine I get that no problem However it then goes on giving, what is in my view a very brief explanation on constructors, saying:

Wouldn't it be nice if you could both create and initialize an object at the same time, PHP allows you to do that with constructors.

And then proceeds to give the following example EXAMPLE 2(UNCLEAR...?)

class Person{
var $name;

function __construct($data)
{ 
  $this->name=$data
}

function set_name($data){
    $this->name=$data;
}

function get_name(){
    return $this->name;
}

}

$dan = new Person;
echo"Friend name is ", $dan->get_name(), ".";

My Question

I would really like to know what is the difference between the two examples above? Also if a beginner practical example could be included it will be enormously appreciated!

Upvotes: 2

Views: 52

Answers (3)

mehany
mehany

Reputation: 4177

Here is a simple answer. First you seem to use javascript var in the above code, so I omit this below. As you can see, the Person is the Object of concern. In the above example, the author is adding only a name to a Person object - but a person could have many other characteristics such as age, date of birth etc... So when a person object is initialized, the Person object will have a place to store its $name in memory. Since the the $name property is private, the above code uses setters and getters to set and get the $name property.

class Person{
    private $name;
    function setName($name){
        $this->name = $name;
    }

    function getName(){
        return $this->name;
    } 

    function setName($name){
        $this->name = $name;
    }
}

$ralph = new Person;
$ralph->set_name('Ralph');
echo 'friend name is '.$ralph->get_name();
// Friend name is Ralph

The second example combines two steps ( btw you seem to have copied the wrong snippet ) the object will accept one parameter $data .

class Person{ private $name;

    function __construct($data)
    { 
        $this->name=$data
    }

    function setName($name){
        $this->name=$name;
    }

    function getName(){
        return $this->name;
    }

}

$dan = new Person('Dan');
echo"Friend name is ", $dan->get_name(), ".";
//Friend name is Dan

A true object Oriented example should look like this:

class Person{
    private $name;

    function __construct($data)
    { 
        $this->name=$data
    }

    function setName($name){
        $this->name=$name;
        return $this; // return $this to allow chaining of method calls

    }

    function getName(){
        return $this->name;
    }

}

Here is the extra, In real world applications, you find stuff like in this example

public function billTo(array $user){
    // Create the Bill To info
    $billto = new AnetAPI\CustomerAddressType();
    $billto->setFirstName($user['firstname']);
    $billto->setLastName($user['lastname']);
    $billto->setCompany($user['company_name']);
    $billto->setAddress($user['address']);
    $billto->setCity($user['city']);
    $billto->setState($user['state']);
    $billto->setZip($user['zipcode']);
    $billto->setCountry($user['country']);
    $billto->setEmail($user['email']);
    $billto->setPhoneNumber($user['phone']);

    return $billto;
}

The above functions creates an instance of the class CustomerAddressType() and stores data from the User array ( passed as a parameter ) in that CustomerAddressType instance. Same as the first example given by the author.

Upvotes: 1

Md. Sahadat Hossain
Md. Sahadat Hossain

Reputation: 3236

The difference between two class is first one you can not initialize data at a time but in second class you can initialize data at a time.

In First Class

class Person{
private $name;
function set_name($data){
    $this->name=$data;
  }

function get_name(){
    return $this->name;
  } 
}
$ralph = new Person; // you can not initialize value here
$ralph->set_name('Ralph');
echo 'friend name is '.$ralph->get_name(); //friend name is Ralph

In second Class

class Person{
private $name;

function __construct($data)
{ 
  $this->name=$data
}

function set_name($data){
    $this->name=$data;
}

function get_name(){
    return $this->name;
}

}

$dan = new Person('Sahadat'); //you can initialize value here
echo"Friend name is ", $dan->get_name(), "."; //Friend name is Sahadat
$dan->set_name('John');
echo"Friend name is ", $dan->get_name(), "."; //Friend name is John

The second way is the best way.

Upvotes: 2

Rob M.
Rob M.

Reputation: 36511

It's no wonder you are confused, that is a very bad example (looks like they forgot to include passing the name to the constructor)! There are also some outdated styles here as well, typically people do not use var anymore for property declarations but declare their scope (public, private, protected, etc.), also some syntax errors (missing semicolon), but that is for another day.

What would have made this a lot more clear is if they actually used the new power of the constructor, which for some reason, they did not. Here is an updated example where the constructor is actually being used:

class Person{
  private $name;

  function __construct($data)
  { 
    $this->name=$data;
  }

  function set_name($data){
      $this->name=$data;
  }

  function get_name(){
      return $this->name;
  }

}

// pass the name to the constructor and you can bypass the set_name call
$dan = new Person("Timothy");

echo "Friend name is ", $dan->get_name(), ".";

This definitely gets much more useful than passing scalars, but hopefully it illustrates what constructors can do a little bit better. Constructors are called automatically whenever you instantiate a new object from a class. Let me know if you have any questions.

Upvotes: 2

Related Questions