3 Replies - 393 Views - Last Post: 17 April 2013 - 07:43 PM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Is It Good Practice To Use One Inherited Class For Properties?

Posted 17 April 2013 - 12:24 AM

So For example this is in some code I use
<?php
interface iPages
{
	public function SetPage($value);
}

interface iTotal
{
	public function ReturnNum();
}

interface iProfiles
{
	public function ReturnProfiles();
}

class CCommentPages implements iPages
{
	private $cFrom;
    private $cTo;
	
	protected function GetPageTo()
	{
		return $this->cTo;
	}
	
	protected function GetPageFrom()
	{
		return $this->cFrom;
	}
	
	public function SetPage($value)
	{
		if ($value == 1)
	    {
		  $this->cFrom = 0;
		  $this->cTo = 20;
	    }
	    else 
	    {
	      $this->cFrom = (20 * ($page-1));
	      $this->cTo = (20 * $page);
	    }
	}
}

class CCommentMainVars extends CCommentPages implements iTotal
{
	private $cNum;
	
	public function ReturnNum()
	{
		if ($this->cNum != null)
		{
		  return $this->cNum;
		}
	}
	
	protected function SetNum($value)
	{
		$this->cNum = $value;
	}
}

class COutputLatest extends CCommentMainVars implements iProfiles
{
	



I'll use it in another class like that or if it doesn't need that other class I'll just use like like this

class COuputRandom extends CCommentPages
{



In other words the fields in CCommentPages for example need to be reused over and over again in other classes. I would think this display good code rereuse in object classes etc. but is this is a good practice? It requires that some property methods be protected and some people have told me you should try to keep everything private.

Also I use classes of course in C# when I work on stuff and I'll use similar ideas there with classes; is that bad in C#? Thanks for your help guys?

Is This A Good Question/Topic? 0
  • +

Replies To: Is It Good Practice To Use One Inherited Class For Properties?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,592
  • Joined: 08-August 08

Re: Is It Good Practice To Use One Inherited Class For Properties?

Posted 17 April 2013 - 12:04 PM

Does it make things easier to maintain?
Was This Post Helpful? 1
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Is It Good Practice To Use One Inherited Class For Properties?

Posted 17 April 2013 - 02:57 PM

View PostCTphpnwb, on 17 April 2013 - 12:04 PM, said:

Does it make things easier to maintain?


Yes because those fields in that class are always going to be needed for related classes for similar use. So if it does make development easier then the answer is yes?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,592
  • Joined: 08-August 08

Re: Is It Good Practice To Use One Inherited Class For Properties?

Posted 17 April 2013 - 07:43 PM

View Postadn258, on 17 April 2013 - 05:57 PM, said:

So if it does make development easier then the answer is yes?

Yes. The question is always whether or not a technique or style actually does make things easier, and that can vary from one person to another and even one project to another. I don't think there's a set answer because it's easier to say what is bad than it is to be sure of what is best.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1