Solved

PHP Classes with Same Name

Posted on 2011-02-18
8
344 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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