PHP Classes with Same Name

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
LVL 3
marcparilloAsked:
Who is Participating?
 
Beverley PortlockConnect With a Mentor Commented:
@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
 
Beverley PortlockCommented:
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
 
marcparilloAuthor Commented:



//----------- 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Beverley PortlockCommented:
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
 
marcparilloAuthor Commented:
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
 
Beverley PortlockCommented:
As long as you don't have two classes with the same name then you will solve your original problem.
0
 
LAMASECommented:
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
 
LAMASEConnect With a Mentor Commented:
@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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.