Jump to content

Learning PHP classes


BahBah

Recommended Posts

Hi

 

I'm trying to build my own User class. It is going ok so far, but I am struggling with inheritence.

 

class user {
  private $profile = array { 'userID', 'firstname', 'surname' };
}

class primaryUser extends user {
  private $additionalFields = array{ 'a', 'b' };
}

 

A primary user is intended to have all of the properties and access to user's methods. However I want to extend the $profile array in user to incorporate the $additionalFields. I am unsure how to do this, and if I am even going about this the right way ? I was intending to go further by extending the user class for any other special user circumstances.

 

The $profile array is used to keep track of what fields are required for a user to be submitted to the database.

 

I hope this makes sense.

 

Thanks for any help you can offer me.

Link to comment
Share on other sites

First off, if you're using the keyword private in your user class any class that extends it will not inherit it. Instead, for this purpose, you should use protected.

 

To add to the profile array in the classes that extend user you can user array_merge. Example:

 

class user
{
protected $profile = array ('userID', 'firstname', 'surname');
}

class primaryUser extends user
{
public function __construct()
{
	$this->profile = array_merge($this->profile, array ('a', 'b'));
	print_r($this->profile);
}
}

 

Output:

Array
(
    [0] => userID
    [1] => firstname
    [2] => surname
    [3] => a
    [4] => b
)

Link to comment
Share on other sites

<?php 

class user {
  protected $profile = array('userID', 'firstname', 'surname');
}

class primaryUser extends user {
  private $additionalFields = array('a', 'b');
  function __construct() {
  	$this->profile = array_merge($this->profile,$this->additionalFields);
  }
  function output() {
  	echo '<pre>';
  	print_r($this->profile);
  	echo '</pre>';
  }
}

$user = new primaryUser();
$user->output();

?>

 

Edit: Was beaten to it :P

Link to comment
Share on other sites

Thank you both :)

 

Instead, for this purpose, you should use protected.

 

Does this mean that if I:

 

$user = new primaryUser();

 

will all the methods in the user class still function correctly. But if I were to make some methods that affect/use the methods or data in the user class I would need to make the methods and variables protected ?

 

Thanks for your assistance.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.