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
Classes Question
Moderator: General Moderators
-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.
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.
Re: Classes Question
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();
}
}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:-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
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)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
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
That's nice for you.I have 4-5 Yrs exp /w PHP and have some up with some pretty innovaitive stuff
Its called a property chain and the example you used is invalid syntax but I'll explain anyway: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 A
{
public $b;
public function __construct()
{
$this->b = new B;
}
}
class B {
public $foo;
}
$a = new A;
$a->b->foo = 10;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)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.
Code: Select all
class A
{
const FOO = 'foo';
const BAR = 'bar';
public function __construct()
{
echo self::FOO;
}
}
echo A::FOO;- Uranium-235
- Forum Newbie
- Posts: 13
- Joined: Tue Aug 08, 2006 3:57 pm
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
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
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK