Solved

PHP Classes with Same Name

Posted on 2011-02-18
8
361 Views
Last Modified: 2012-05-11
I'm trying to create the architecture for a new website using OOP and I have a question about creating and using class names.  

I already have an Events Class and a Staff Class and there are similarities between them. They each have Titles, Locations, Documents, and Descriptions and I would like to create separate classes for each to handle their unique logic.  

So, for example, the Events Class would have Title, Location, Document subclasses and the Staff Class would also have Title, Location, Document subclasses.    A Title Class would extend Events and a Title Class would extend Staff, etc.

When I try to implement that, I get the error "can't re-declare classes..."

So, my question is:  Is there a way to do this (possibly with namespaces?) or am I approaching it incorrectly?

Thank you,
Marc
0
Comment
Question by:marcparillo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34928892
Please post your code so we can see what you are doing. If the classes have separate names then you shoud not be gettng a redeclaration error. It is possible that you have included files more than once and if so the use include_once or require_once rather than include or require
0
 
LVL 3

Author Comment

by:marcparillo
ID: 34928961



//----------- Engine.php

require_once('Events.php');
require_once('Staff.php');

$staff = new Staff();
$events = new Events();


//---------- Events.php 

class Events {
		
	public function __construct() {
		
		print __CLASS__ . ' constructor';
		
	}
			
}

class Title extends Events {
	
	private $var = 2;
	
	public function getVar() {
		
		echo $this->var;	
		
	}
	
}

//---------- Staff.php

class Staff {
		
	public function __construct() {
		
		print __CLASS__ . ' constructor';
		
	}
			
}

class Title extends Staff {
	
	private $var = 2;
	
	public function getVar() {
		
		echo $this->var;	
		
	}
	
}

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34929277
The problem is that you have 2 class Title declarations and I think you have the declarations backwards. Make it more like this

//----------- Engine.php

require_once('Title.php');
require_once('Events.php');
require_once('Staff.php');

$staff = new Staff();
$events = new Events();

// ------------ Title.php


class Title {

    private $var = 2;

    public function getVar() {

        echo $this->var;

    }

}


//---------- Events.php

class Events extends Title {

    public function __construct() {

        print __CLASS__ . ' constructor';

    }

}

//---------- Staff.php

class Staff extends Title {

    public function __construct() {

        print __CLASS__ . ' constructor';

    }

}

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 3

Author Comment

by:marcparillo
ID: 34929413
Thank you for the suggestion, but I don't think that's what I'm looking for. I wanted to organize my Title, Description ... etc subclasses under their respective parent class.

I think I'll just do it like this (unless you can suggest a different way).

That way all of the Title logic falls under its parent class.
class Events {
  // Main Events code
}
class Events_Title extends Events {
  // Logic for Title
}
class Events_Description extends Events {
  // Logic for Description
}



class Staff {
  // Main Staff code
}
class Staff_Title extends Staff {}
class Staff_Description extends Staff {}

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34929563
As long as you don't have two classes with the same name then you will solve your original problem.
0
 
LVL 4

Expert Comment

by:LAMASE
ID: 34930843
The point is: what do you mean with "subclass"?

In my opinion you need an ATTRIBUTE of Staff and Events class that can be an object of type "Title"


class Title {
...
}

class Staff {
   var $title;
   ...
}

class Events {
   var $title;
   ...
}

// ...other stuff

$event = new Event();
$event->title = new Title();

$staff = new Staff();
$staff->title = new Title();

$event->title->someMethod();

Open in new window

0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 34931922
@LAMASE - yes, that's what I was hinting at a few posts up. Things like 'title' and 'description'  are commonly properties of a class and not classes in their own right. For myself I would define common properties in a base class and then either encapsulate them in other classes or extend them to derive child classes.

class Base {
     public $title;
     public $description;

     function __construct( $t, $d ) {
          $this->title = $t;
          $this->description = $d;
     }

     .... other code...
}



class Event extends Base {

     function __construct( $t, $d ) {
          parent::__construct( $t, $d );
     }

     ... etc
}
0
 
LVL 4

Assisted Solution

by:LAMASE
LAMASE earned 250 total points
ID: 34932052
@bportlock
We can mix things together for the best result:

class Title {
...
}
class Description {
...
}


class Base {
     public $title;
     public $description;

     function __construct( $t, $d ) {
          $this->title = new Title($t);
          $this->description = new Description($d);
     }

     .... other code...
}


class Event extends Base {
     function __construct( $t, $d ) {
          parent::__construct( $t, $d );
     }
     ... etc
}

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question