Forum Topic: PHP globals: the class version

(235 views • 13 replies)

This topic is 1 page long.

<< < > >>
Misunderstood

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 6/7/09 10:19 PM

Momo-the-Monkey EVIL LEVEL 34

Sign-Up: 10/15/05

Posts: 3,250

I have a class defined. $class = new class. Blah blah blah. And then I have a function on the same page.
I try to call the $class variable from inside my nifty function. Nothing. Just a fatal error about calling a non-object member function.

So I know what that means. $class is no longer defined inside the function. Grug. Re-establishing $class as new class inside the function is a way to prevent this. But unfortunately, I have more functions than I would want to redefine $class as new class. That would be almost defeat my purpose and would send me back to just defining the class as hefty function.

Is there any workaround to this? I've done my research, and have found nothing. I hang my head low, but before I switch back to function work I want to see if I could actually make this work. Any suggestions?

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

Loccie

Reply To Post Reply & Quote

Posted at: 6/8/09 10:07 AM

Loccie NEUTRAL LEVEL 16

Sign-Up: 02/27/04

Posts: 1,059

So inside your function it no longer knows your class? Did you create this function before you wrote your class? I mean like this:

include 'myFunctions.php';
include 'myClasses.php';

If so that could be the problem.


None

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 6/8/09 11:36 AM

Momo-the-Monkey EVIL LEVEL 34

Sign-Up: 10/15/05

Posts: 3,250

Nope. My classes are called before my functions.

Class {
blah
}

function blah(){
blah
}

The class is the first declared on the page. And so is the PDO class, I have just noticed. I created a blank function and tried to execute database commands via PDO, and they did not work. Also giving me the Call to a member function is non-object.

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

VigilanteNighthawk

Reply To Post Reply & Quote

Posted at: 6/8/09 12:41 PM

VigilanteNighthawk LIGHT LEVEL 03

Sign-Up: 02/13/03

Posts: 1,692

I'm having a little bit of trouble following. Could you post the code for the class and function please?


None

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 6/8/09 12:56 PM

Momo-the-Monkey EVIL LEVEL 34

Sign-Up: 10/15/05

Posts: 3,250

The code is unnecessary. All I need to know is how to do this

$newclass = new myClass;
class myClass {
    function myFunc(){
        echo 'my class works';
    }
}

function outsideFunc(){
    $newclass->myFunc();
}
outsideFunc();

That does not work. An error, "Call to a member function myFunc() on a non-object" results. I have looked into define() which only uses scalar values, I have tried global variables, which I still have to define in every function I want to use them in, and obviously the code above does not work. I am slowly believing I have no other option but to resort back to function work for the deep stuff. V_V

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 6/8/09 04:32 PM

Afro-Ninja EVIL LEVEL 37

Sign-Up: 03/02/02

Posts: 13,466

At 6/8/09 12:56 PM, Momo-the-Monkey wrote: That does not work. An error, "Call to a member function myFunc() on a non-object" results. I have looked into define() which only uses scalar values, I have tried global variables, which I still have to define in every function I want to use them in, and obviously the code above does not work. I am slowly believing I have no other option but to resort back to function work for the deep stuff. V_V

scoping is a bit different in php
http://us2.php.net/manual/en/language.va riables.scope.php

if you're not passing $newclass to the function then you need to do

function outsideFunc(){
    global $newclass;
    $newclass->myFunc();
}
BBS Signature

Crying

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 6/8/09 08:08 PM

Momo-the-Monkey EVIL LEVEL 34

Sign-Up: 10/15/05

Posts: 3,250

*Sigh* I was afraid of that. I do have a lot of functions, which means defining every variable inside every function is going to be a hassle. Oh well. I guess it's back to more functions for me.

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

VigilanteNighthawk

Reply To Post Reply & Quote

Posted at: 6/8/09 09:00 PM

VigilanteNighthawk LIGHT LEVEL 03

Sign-Up: 02/13/03

Posts: 1,692

I'm not sure why you need the global access to the class, but I sounds to me like, depending on what you are trying to accomplish, either the factory and singleton patterns are what you need. This link will explain their implementations: http://us3.php.net/manual/en/language.oo p5.patterns.php


None

DearonElensar

Reply To Post Reply & Quote

Posted at: 6/9/09 06:59 AM

DearonElensar LIGHT LEVEL 18

Sign-Up: 06/10/02

Posts: 1,731

You can also create a class that holds instances of every other class

<?php
class classes
{
         public $otherclass;

          public function __construct()
          {
                    $this->otherclass = new otherclass;
          }
}

function func($classes)
{
          $classes->otherclass->classfunction();
}

func($classes);

But it's more a quick-and-dirty solution, do you really need separate function from the classes and if so can't you pass them through the arguments?

BBS Signature

None

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 6/9/09 09:29 AM

Momo-the-Monkey EVIL LEVEL 34

Sign-Up: 10/15/05

Posts: 3,250

Factory patterns and singletons would work if I was not using the class in a function called outside of the class.

At 6/9/09 06:59 AM, DearonElensar wrote: You can also create a class that holds instances of every other class

But it's more a quick-and-dirty solution, do you really need separate function from the classes and if so can't you pass them through the arguments?

For the most part, I could do away with a few functions, but there are still those that I would have to pass the class to. Yes, I could pass the class to them, but I was just wondering if there was a way to make the class "global" for all functions with only a few lines of code called outside of the function, so I didn't have to recall every class. But I think I am going to have to pass the classes and get rid of a few functions.
Thanks oodles for the help though. I had forgotten I could pass classes.

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

DearonElensar

Reply To Post Reply & Quote

Posted at: 6/9/09 01:02 PM

DearonElensar LIGHT LEVEL 18

Sign-Up: 06/10/02

Posts: 1,731

At 6/9/09 09:29 AM, Momo-the-Monkey wrote: For the most part, I could do away with a few functions, but there are still those that I would have to pass the class to. Yes, I could pass the class to them, but I was just wondering if there was a way to make the class "global" for all functions with only a few lines of code called outside of the function, so I didn't have to recall every class. But I think I am going to have to pass the classes and get rid of a few functions.
Thanks oodles for the help though. I had forgotten I could pass classes.

Well... not 100% sure but i think you can do things like Class::Function inside a function, so if there isn't a need to declare it (just functions and no shared variables for example) you can try that.

BBS Signature

None

VigilanteNighthawk

Reply To Post Reply & Quote

Posted at: 6/9/09 02:29 PM

VigilanteNighthawk LIGHT LEVEL 03

Sign-Up: 02/13/03

Posts: 1,692

At 6/9/09 09:29 AM, Momo-the-Monkey wrote: Factory patterns and singletons would work if I was not using the class in a function called outside of the class.

They should work just fine so long as the method used to obtain the class is static. In fact, one of the reasons for the singleton is to provide global access without relying on global variables. Here is a quick and dirty example with a singleton:

class Single{
          private static $instance;
   
         private function __construct(){
              //stuff
        } 

        public static function getInstance(){
                   if(self::$instance == null){
                            self::$instance = new Single();
                  }

        }
       
        public function doStuff(){
                 //do stuff
        }

}

function callIt(){
      //provides access to the instance of the object without globals
      $single = Single::getInstance();
      $single->doStuff();

}

I'm also not quite following why you haven't just used the global


Elated

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 6/9/09 11:35 PM

Momo-the-Monkey EVIL LEVEL 34

Sign-Up: 10/15/05

Posts: 3,250

At 6/9/09 01:02 PM, DearonElensar wrote: Well... not 100% sure but i think you can do things like Class::Function inside a function, so if there isn't a need to declare it (just functions and no shared variables for example) you can try that.

Oh oh oh, OOOOKaaaaay. I understand now.

At 6/9/09 02:29 PM, VigilanteNighthawk wrote: I'm also not quite following why you haven't just used the global

Thanks to the both of you I think I can wrap my head around this. Using the method call property (MyClass::classFunc()), calling them that way, will work inside of functions. Oh, thank you so much for putting up with my unknowledgableness. Hahaha, thanks and thanks again. I shall research more into singletons and patterns.

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

DearonElensar

Reply To Post Reply & Quote

Posted at: 6/10/09 06:52 AM

DearonElensar LIGHT LEVEL 18

Sign-Up: 06/10/02

Posts: 1,731

At 6/9/09 11:35 PM, Momo-the-Monkey wrote: Oh, thank you so much for putting up with my unknowledgableness. Hahaha, thanks and thanks again. I shall research more into singletons and patterns.

No problem, it's good to see that it "worked out" :)

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 01:54 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!