TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-17-2007, 01:10 PM   #1 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default 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.
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-17-2007, 01:24 PM   #2 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Method chaining!

PHP5 Method Chaining
Haris is offline  
Reply With Quote
Old 11-17-2007, 02:09 PM   #3 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Quote:
Originally Posted by Haris View Post
Method chaining!

PHP5 Method Chaining
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.
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-17-2007, 02:20 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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'); 
Salathe is offline  
Reply With Quote
Old 11-17-2007, 02:26 PM   #5 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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)
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-17-2007, 02:34 PM   #6 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 12
Thanks: 0
DarkPrince11 is on a distinguished road
Default

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();
?>
Send a message via AIM to DarkPrince11 Send a message via MSN to DarkPrince11
DarkPrince11 is offline  
Reply With Quote
Old 11-17-2007, 03:38 PM   #7 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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
Village Idiot is offline  
Reply With Quote
Old 11-17-2007, 04:58 PM   #8 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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?
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-17-2007, 05:12 PM   #9 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Morishani View Post
Is it ok to work like that?
In short, yes.
Salathe is offline  
Reply With Quote
Old 11-17-2007, 05:23 PM   #10 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
In short, yes.
But is there any better solution? Or let me put it that way : What is the best way to do this?
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-26-2007, 10:16 PM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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)
sketchMedia is offline  
Reply With Quote
Old 11-28-2007, 08:11 AM   #12 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
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" :)
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-28-2007, 10:47 PM   #13 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:15 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design