 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
11-17-2007, 01:10 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 32
Thanks: 5
|
Class inside a class(?)
Hey all, I have a question for you.
I Have one class, lets call it for example "table". inside that class I want to insert another class, for example "columns".
but I don't need the "columns" class outside of the "table" class, only inside it, and only 1 columns class will be needed within a table class.
PHP Code:
//Good code :
$table = new table();
$table->columns->add("asdf");
//Bad code :
$columns = new columns();
$columns->add("asdf");
How can i do that? thanks.
|
|
|
11-17-2007, 01:24 PM
|
#2 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
|
|
|
|
11-17-2007, 02:09 PM
|
#3 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 32
Thanks: 5
|
Quote:
Originally Posted by Haris
|
Thanks but that is not what i'm looking for.
I'm talking about that the "columns" class will be only available within the "tables" class, and cannot be available outside of the "tables" class.
|
|
|
11-17-2007, 02:20 PM
|
#4 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Inside your table class, just add a member variable which is an instance of the columns class.
For example (only an example!):
PHP Code:
class table { public $columns; public function __construct() { $this->columns = new columns(); } }
// Then you can do: $table = new table(); $table->columns->add('asdf');
|
|
|
|
11-17-2007, 02:26 PM
|
#5 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 32
Thanks: 5
|
Quote:
Originally Posted by Salathe
Inside your table class, just add a member variable which is an instance of the columns class.
For example (only an example!):
PHP Code:
class table { public $columns; public function __construct() { $this->columns = new columns(); } }
// Then you can do: $table = new table(); $table->columns->add('asdf');
|
Thats one nice way, but after that you can also do :
PHP Code:
//main code, not inside a class : $columns = new columns(); $columns->add("asdf");
And I don't want that, but if there is no way to delete this privilege then I may use your way.
So, Salathe, what do you say?
Also, if i use your way, is there anyway to get to the "parent element" from columns class? (so i can reach "table" class from "columns" class functions)
|
|
|
11-17-2007, 02:34 PM
|
#6 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Posts: 12
Thanks: 0
|
If you create a columns object inside the table object,than you cannot access that object directly out of the object.
For example
PHP Code:
<?php
class table {
public $columns;
public function __construct() { $this->columns = new columns(); } }
//Will Work// $table = new columns(); $table->columns->function();
//Will Not Work Unless You Create A New Columns Object ($columns = new columns()// $columns->function(); ?>
|
|
|
11-17-2007, 03:38 PM
|
#7 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Why dont you just inherit an abstract class?
PHP Code:
<? abstract class a { public function func($param) { echo "$param"; } }
class b extends a { public $variable; }
$instance = new b; $instance->variable = "hello there"; $instance->func($instance->variable); ?>
Class A cannot be called as an instance because it is abstract, therefore then only class that can use func() is B, or any class that extends A.
Note: You must have PHP5 to do this, php4 doesnt support abstraction if I recall
|
|
|
|
11-17-2007, 04:58 PM
|
#8 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 32
Thanks: 5
|
Quote:
Originally Posted by Salathe
Inside your table class, just add a member variable which is an instance of the columns class.
For example (only an example!):
PHP Code:
class table
{
public $columns;
public function __construct()
{
$this->columns = new columns();
}
}
// Then you can do:
$table = new table();
$table->columns->add('asdf');
|
Salath, I think I'm gonna choose your solution to this problem, but now i've got another problem - how to get to the table object from the columns object,
what i did is something like this, and i want to know if it's correct :
PHP Code:
class inner_class
{
private $parent;
function __construct($parent)
{
$this->parent = $parent;
}
public function a()
{
return $this->parent->name;
}
}
class outer_class
{
public $name;
public $iClass;
function __construct()
{
$this->name = "asdf";
$this->iClass = new inner_class($this);
}
}
$a = new outer_class();
echo $a->iClass->a();
Is it ok to work like that?
|
|
|
11-17-2007, 05:12 PM
|
#9 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Morishani
Is it ok to work like that?
|
In short, yes.
|
|
|
|
11-17-2007, 05:23 PM
|
#10 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 32
Thanks: 5
|
Quote:
Originally Posted by Salathe
In short, yes.
|
But is there any better solution? Or let me put it that way : What is the best way to do this?
|
|
|
11-26-2007, 10:16 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
not looking at this properly, i would say the only other way of doing it is to use an abstract.
Just to clear somthing up, doesnt a table have columns anyway (thinking from an object point of view, a table has columns and rows), so the table object should have the ability to add columns so i dont see the use in creating an extra object for columns, im sorry if im just being a bit thick if that isnt the case, i must be on one of my stupid head days.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
11-28-2007, 08:11 AM
|
#12 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 32
Thanks: 5
|
Quote:
Originally Posted by sketchMedia
not looking at this properly, i would say the only other way of doing it is to use an abstract.
Just to clear somthing up, doesnt a table have columns anyway (thinking from an object point of view, a table has columns and rows), so the table object should have the ability to add columns so i dont see the use in creating an extra object for columns, im sorry if im just being a bit thick if that isnt the case, i must be on one of my stupid head days.
|
You're preety right there. Good point. That must be the reason that I couldn't find a good solution for my "none exists problem" :)
|
|
|
11-28-2007, 10:47 PM
|
#13 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
I can relate to that i have been there myself mate, when you don't think things through properly and end up making a relatively simple task 20 times harder and then tearing your hair out because there isn't any articles to help :D.
Good luck with the rest of the script.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|