TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Class inside a class(?) (http://www.talkphp.com/advanced-php-programming/1486-class-inside-class.html)

Morishani 11-17-2007 01:10 PM

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.

Haris 11-17-2007 01:24 PM

Method chaining!

http://www.talkphp.com/showthread.ph...light=Chaining

Morishani 11-17-2007 02:09 PM

Quote:

Originally Posted by Haris (Post 4313)

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.

Salathe 11-17-2007 02:20 PM

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'); 


Morishani 11-17-2007 02:26 PM

Quote:

Originally Posted by Salathe (Post 4319)
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)

DarkPrince11 11-17-2007 02:34 PM

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();
?>


Village Idiot 11-17-2007 03:38 PM

Why dont you just inherit an abstract class?

PHP Code:

<?
abstract class a
{
    public function 
func($param)
    {
        echo 
"$param";
    }
}

class 
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

Morishani 11-17-2007 04:58 PM

Quote:

Originally Posted by Salathe (Post 4319)
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?

Salathe 11-17-2007 05:12 PM

Quote:

Originally Posted by Morishani (Post 4326)
Is it ok to work like that?

In short, yes.

Morishani 11-17-2007 05:23 PM

Quote:

Originally Posted by Salathe (Post 4327)
In short, yes.

But is there any better solution? Or let me put it that way : What is the best way to do this?

sketchMedia 11-26-2007 10:16 PM

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.

Morishani 11-28-2007 08:11 AM

Quote:

Originally Posted by sketchMedia (Post 4600)
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" :)

sketchMedia 11-28-2007 10:47 PM

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.


All times are GMT. The time now is 07:31 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0