/    Sign up×
Community /Pin to ProfileBookmark

OOP questions – calling variable from object within an object

how do you call a variable and can you direct me to a way/link, if there is one
to good variable passing practices.

and, yeah, to the question…how do you call the variable from an object declared within anotehr object…

please explain, all that you can…? thanks….

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@bokehMay 24.2007 — Can you post an example of what you are attempting?
Copy linkTweet thisAlerts:
@php_hazard_01authorMay 24.2007 — well, its like this

[code=php]<?php

class foo1{

function fooFunction(){
global $class;
$class = new foo2();

}

function foofunction2(){
global $class;
$class->var_from_foo2; // the variable to be called from the outside.

}

}

?>[/code]




example, i have another page, where it requires the[B] $class->var_from_foo2[/B] to be called...
Copy linkTweet thisAlerts:
@bokehMay 24.2007 — What are you thinking using global variables inside a class. A class should be portable, not tied in any way to a procedural script.
Copy linkTweet thisAlerts:
@NogDogMay 24.2007 — [code=php]
<?php

require_once "foo2.class.php";

class foo1{

var $class;

function foo1()
{
$this->class = new foo2();
}

function foofunction2()
{
echo $this->class->var_from_foo2;
}

}

?>
[/code]
Copy linkTweet thisAlerts:
@php_hazard_01authorMay 25.2007 — @bokeh - oh?..sorry then, im not really on OOP person...so, the way im coding is bad ryt?..

i need to redo it?...

thanks bokeh....

@NogDog - hey nD...so, how can i all that variable from another page?...

do i call it by $foo1->class->var_from_foo2;


im so s2pid, i dont know what to do....
Copy linkTweet thisAlerts:
@NogDogMay 25.2007 — [code=php]
$foo1 = new foo1();
echo $foo1->class->var_from_foo2;
[/code]
Copy linkTweet thisAlerts:
@php_hazard_01authorMay 25.2007 — i know this sounds a little of the proper coding practice, but how do you call a variable from inside another a class.....

hehe...?

from the new foo1,

$foo1->class->var_from_foo2 is called from class foo3;
Copy linkTweet thisAlerts:
@NogDogMay 25.2007 — Without really understanding what you are trying to accomplish, I'm not sure what the correct answer is. Your basic choices are:

  • 1. Instantiate an object of the 2nd class within the first (as per my previous example).


  • 2. Pass an already instantiated object as a parameter to one of the class's methods (including its constructor).


  • 3. Inherit the property from one class into another via the [b]extends[/b] keyword.


  • To say which is the best way (or another way that I haven't thought of right now, is something I cannot say without understanding why you want to do it.
    Copy linkTweet thisAlerts:
    @php_hazard_01authorMay 25.2007 — im really sorry ND....but you helped, im not really good at putting up questions...?
    ×

    Success!

    Help @php_hazard_01 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...