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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.