Solved

PHP Classes with Same Name

Posted on 2011-02-18
8
320 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
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
Comment Utility



//----------- 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
Comment Utility
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
 
LVL 3

Author Comment

by:marcparillo
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
@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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now