Jump to content

OOP / OOP & DEFINE


seventheyejosh

Recommended Posts

Hello all! A few more questions.

 

If my index.php is as follows (all this is theoretical, and not syntax checked):

 


session_start();

require_once "classes/class_a.php";
require_once "classes/class_b.php";

 

and they were as follows

 

a:

 


class a{

protected $user='Josh';

protected function something(){

}

}

 

and b was

 


class b extends class a{

public function another(){

echo $this->user;

something();

}


}

 

would that work?

 

or would I have to do:

 


require_once "class_a.php";
require_once "class_b.php";

$a=new a();
$b=new b();

 

in my index.php?

 

or would any of this work if the classes arent in the same file?

 

also, is it a faux pas to use DEFINE in OOP?

 

and I'm almost sure it is, but global is frowned on as well right?

 

Thanks so much!

Link to comment
https://forums.phpfreaks.com/topic/174088-oop-oop-define/
Share on other sites

The first way would work, but you would have to do $this->something() instead of just something().

 

 

Some languages figure out method scope on their own (like Java for example), but in PHP you have to put the $this.

 

 

Also, if you need to define something, you could always use a class constant.

 

 

class SomeClass {

    public const SomeConst = 5;

}

 

echo SomeClass::SomeConst;

//Outputs 5

Link to comment
https://forums.phpfreaks.com/topic/174088-oop-oop-define/#findComment-917750
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.