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
https://forums.phpfreaks.com/topic/185696-learning-php-classes/
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
https://forums.phpfreaks.com/topic/185696-learning-php-classes/#findComment-980546
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
https://forums.phpfreaks.com/topic/185696-learning-php-classes/#findComment-980548
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
https://forums.phpfreaks.com/topic/185696-learning-php-classes/#findComment-980549
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.