Classes Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Uranium-235
Forum Newbie
Posts: 13
Joined: Tue Aug 08, 2006 3:57 pm

Classes Question

Post by Uranium-235 »

I have 4-5 Yrs exp /w PHP and have some up with some pretty innovaitive stuff, but only within the past few weeks been messing with classes, and have a few questions among others

-Q1

I've seen a few scripts that have multiple classes defined, and they are called into eachother

$foo = bar -> bar2 -> bar3("whatever");

I'm somewhat confused on how to do this, and the whole point of this. Can it be used to allow some classes to see other class varibles, etc?

-Q2

I understand refrences, but am still somewhat confused by =&. And why do I see it used in so many classes? I read the PHP doc on it and am still somewhat confused


-Q3

Is it 'good form' to make global constants in a class script? It's hard to explain, I use certain constants in the class functions themself, and when they are used.

define("FOO", 0);
define("BAR", 1);

for the purpose of setting certain attributes when the class functions are used in the script using the class.

that's all for now,
thanks
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

-Q1

A class is simply an object. Objects can have member objects.
so in this case, you have bar as an object and it has objects which has objects. ^,^
in this situation, the class is calling its own variable (another object) not other class variables.

bar can not call bar3.


lol, im bad at explaining.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Classes Question

Post by Luke »

Uranium-235 wrote:I've seen a few scripts that have multiple classes defined, and they are called into eachother

$foo = bar -> bar2 -> bar3("whatever");

I'm somewhat confused on how to do this, and the whole point of this. Can it be used to allow some classes to see other class varibles, etc?

Code: Select all

class classOne{
    function doSomething(){
        // do stuff
    }
}
class classTwo{
    function classTwo($instanceOfClassOne){
        $this->classOne = $instanceOfClassOne;
        $this->classOne->doSomething();
    }
}
Class two accepts class one as a param, and then uses it's method... also could be used in about a million other ways... (ahem design patterns)
Uranium-235 wrote:-Q2

I understand refrences, but am still somewhat confused by =&. And why do I see it used in so many classes? I read the PHP doc on it and am still somewhat confused
Because PHP4 or older makes a copy of an object instead of just passing a reference to an object unless you use & (PHP5 fixes this issue)
Uranium-235 wrote:--Q3

Is it 'good form' to make global constants in a class script? It's hard to explain, I use certain constants in the class functions themself, and when they are used.

define("FOO", 0);
define("BAR", 1);

for the purpose of setting certain attributes when the class functions are used in the script using the class.

that's all for now,
thanks
It really depends... classes are meant to be independant of environment, so no, using constants would not be considered good practice, but sometimes it makes sense to. I still use constants for config variables (at least during development)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I have 4-5 Yrs exp /w PHP and have some up with some pretty innovaitive stuff
That's nice for you.
I've seen a few scripts that have multiple classes defined, and they are called into eachother
$foo = bar -> bar2 -> bar3("whatever");
I'm somewhat confused on how to do this, and the whole point of this. Can it be used to allow some classes to see other class varibles, etc?
Its called a property chain and the example you used is invalid syntax but I'll explain anyway:

Code: Select all

class A
{
    public $b;
    public function __construct()
    {
        $this->b = new B;
    }
}
class B {
    public $foo;
}

$a = new A;
$a->b->foo = 10;
Here object $a, an instance of class A contains a property ($b) which is an instance of B which in turn as a property $foo. Why do this? Because say you created a class to store a table you might want to store rows in it and columns in them etc. etc.
Is it 'good form' to make global constants in a class script? It's hard to explain, I use certain constants in the class functions themself, and when they are used.
define("FOO", 0);
define("BAR", 1);
for the purpose of setting certain attributes when the class functions are used in the script using the class.
Constants are a good idea, as they standardize data however using them as you described is most definately a bad idea. What you have done is to make your class dependent on constants outside of itself which means you couldn't easy use the class in other applications without bringing the constants with it too. A solution to this is class constants (PHP 5 only)

Code: Select all

class A
{
    const FOO = 'foo';
    const BAR = 'bar';
    public function __construct()
    {
        echo self::FOO;
    }
}
echo A::FOO;
User avatar
Uranium-235
Forum Newbie
Posts: 13
Joined: Tue Aug 08, 2006 3:57 pm

Post by Uranium-235 »

sorry the intention wasn't to brag (which I really can't, compaired to some people here I suck @ PHP quite throughly), merly implying I'm not a noob and I would understand a detailed explination

I've just been looking @ the Excel XML Parser Class @ PHPclasses and am trying to understand some of it cause one of my future projects might be structured in a similar manner.

I believe I understand what you're saying. I'll mess around with it some more, see if I can get a better grasp

I am working with PHP4 here, not 5, something I should of said. When I make constants I try to name them as specific to the class as possible so they don't have any conflicts with other included scripts.

I'm not going to start writing stuff for PHP5, until PHP4, becomes what PHP 3 is today (actually more like last year)

thanks for the help everyone
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

might be a good idea to study php 5 if you are just learning the concepts of oop though because php4 doesn't really get them right.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah PHP 4 is a bit of joke for OOP tbh
Post Reply