?
Solved

PHP Classes with Same Name

Posted on 2011-02-18
8
Medium Priority
?
367 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

771 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