Jump to content

OOP question


dennismonsewicz

Recommended Posts

thanks.

 

what is the difference between public and private?

 

Public: can be invoked by code outside of the class itself.

Private: can only be invoked within the class.

 

Example:

class Example
{
   private $SSN; //Social Security Number...it shouldn't be freely available to the rest of the system
   private $name;

   public function __construct($SSN, $name)
   {
      $this->SSN = $SSN;
      $this->name = $name;
   }

   public function getSSN()
   {
      return $this->SSN;
   }

   public function getName()
   {
      return $this->name;
   }

   public function setName($name)
   {
      $this->name = $name;
   }
}

 

So, let's say the main code is:

$example = new Example("000-00-0000", "John Doe");

$example->SSN = "123-45-6789"; //<-- ERROR
$mySSN = $example->SSN; //<-- ERROR
$example->name = "Angelina Jolie"; //<-- ERROR
$myName = $example->name; //<-- ERROR

$mySSN = $example->getSSN(); //<-- SUCCESS!
$example->setName("Angelina Jolie"); //<-- SUCCESS!
$myName = $example->getName(); //<-- SUCCESS!

 

So, you're probably wondering what the point is.  One of the core tenets of OOP is of encapsulation.  Objects should encapsulate data.  This data should only be accessed through clear lines of communication to ensure that no ambiguity or errors creep into the data itself.  To put it bluntly, it's dangerous to keep things publicly accessible because you're not guaranteed that the data is safe.  By forcing your (or another's) code to use these clearly defined rules of communication, you help ensure that the object is used in the proper way.

 

The same applies to private methods, as well.  These tend to be bookkeeping methods, or methods that do a lot of the grunt work that the rest of the public world shouldn't concern itself with.

Link to comment
Share on other sites

Hey guys, I am learning OOP too but I have tried to do my due diligence and read a lot of books on OOP design and concepts. I have a quick question that I hope someone can help me with.

 

The question has to do with "$this" pointer in a class. I know how to use it, but I want to UNDERSTAND how it works.

Below  are two examples that I'd like to know if someone can explain to me the difference.

 

Both examples have the property "$fullname" as public as the class attribute.

 

public function setName($name) {

           $this->fullname = $name;
}

public function setName($name) {

           $fullname = $name;
}


 

 

so basically I know that the first function sets the property value of $fullname to whatever is passed to the function via $name.

 

But in the second function which is (WRONG), my question is, why is it wrong?

 

 

 

 

Link to comment
Share on other sites

class someClass {
   // this is is a property in your class. A property is
   // a fancy way of saying "variable inside a class." 
   var $someVar;

   // this is a method.  A method is a fancy way of 
   // saying function inside a class
   function foo () {
      // this will assign "something" to the $somevar property in your class
      // 'this' is an alias of the class name, so that php knows to assign it
      // to the property (variable) inside this class (someClass)
      $this->someVar = "something";
   }

   // another method
   function bar () {
      // this will assign "something" to a variable local
      // only to this method.  $somevar is a variable that only exists inside
      // this function, and is not related to the property $somevar in the class
      $someVar = "something";
   }
}

 

Link to comment
Share on other sites

Ohh thank you Crayon Violent....(that's a funny name what does it mean?)

 

You see, I've read 3-4 different books as well as many articles. Many talk about "$this" as being a pointer, or keyword and I suppose they are correct.

 

Your explanation of it being an "ALIAS" makes a lot more sense. I now understand!!

 

One more quick question, since "$someVar" becomes a local variable, in the second function example, does "$someVar" have to be declared first like so within the local function...

And if you DONT declare it and try to use it, will PHP implicitly create it or will output a fatal error?

 

public $something

 

 

Also, are you aware if PHP uses default constructors for subclasses if the parent class has a defined constructor that is implemented?

 

 

Link to comment
Share on other sites

Ohh thank you Crayon Violent....(that's a funny name what does it mean?)

 

The origin of my handle is an epic saga, and cannot possibly be contained in a single post. 

 

You see, I've read 3-4 different books as well as many articles. Many talk about "$this" as being a pointer, or keyword and I suppose they are correct.

 

Your explanation of it being an "ALIAS" makes a lot more sense. I now understand!!

Yes, it is a pointer.  IMO the problem with a lot of books and tutorials is that they tend to explain abstract things by using more abstract words, so most people only understand what they are saying if they already knew the topic in the first place.

 

One more quick question, since "$someVar" becomes a local variable, in the second function example, does "$someVar" have to be declared first like so within the local function...

And if you DONT declare it and try to use it, will PHP implicitly create it or will output a fatal error?

PHP is a loosely typed language.  It does not require you to declare variables.  it will not give you a fatal error if you don't, but it will give you a warning.  If you don't see a warning from not declaring variables, it is because your php settings suppress it (or you suppressed it in the code somewhere). 

 

You do, however, have to declare a class property (a class variable), and trying to use a property that doesn't exist will give you a fatal error.

 

Also, are you aware if PHP uses default constructors for subclasses if the parent class has a defined constructor that is implemented?

I'm not 100% sure what you mean, so I'll try to answer this as I understand it.  If I hit the nail on the head, awesome.  If not, please clarify.  Let's say you have a class and you want to make another class that extends that first class.  The first class would be that second class's parent class, and that second class will be a child of the parent class.  The child class inherits everything from the parent class, except for things specifically flagged not to be inherited (you do this by putting "private" in front of the property or method in the parent class). 

 

By default, everything is inherited, even the constructor, so when you create an object from the child class, it will call the parent class's constructor.  If the parent class has a constructor, and you make a constructor class in your child class, the child's class will override the parent class's constructor (the same override happens if you name any property or function the same thing as something in the parent class).  If you do not want this to happen - that is, if you want both the parent and child constructors to be executed, you need to call the parent's constructor class inside the child's constructor class. 

 

Newcomers to OOP find it large and scary and don't really know where to begin.  Let me sum it up in one sentence for you:

 

Classes add another layer of scope to the environment.

 

The End.  If you already understand how a functions work (noticing that a chunk of code is being reused over and over so you group it together, slap a label on it and just call it by name, passing/returning variables, variable scope, etc..) then you are 99% there as far as classes are concerned. 

 

Class properties are simply variables that operate within a class.  Class methods are regular run of the mill functions that operate within a class.  There are lots of different features and syntaxes and "permissions" for property/method accessibility that go along with it; it is a very robust upgrade to what functions offer, but it all still boils down to that single statement.

 

 

Link to comment
Share on other sites

I'm not 100% sure what you mean, so I'll try to answer this as I understand it.  If I hit the nail on the head, awesome.  If not, please clarify.  Let's say you have a class and you want to make another class that extends that first class.  The first class would be that second class's parent class, and that second class will be a child of the parent class.  The child class inherits everything from the parent class, except for things specifically flagged not to be inherited (you do this by putting "private" in front of the property or method in the parent class). 

 

By default, everything is inherited, even the constructor, so when you create an object from the child class, it will call the parent class's constructor.  If the parent class has a constructor, and you make a constructor class in your child class, the child's class will override the parent class's constructor (the same override happens if you name any property or function the same thing as something in the parent class).  If you do not want this to happen - that is, if you want both the parent and child constructors to be executed, you need to call the parent's constructor class inside the child's constructor class. 

 

Newcomers to OOP find it large and scary and don't really know where to begin.  Let me sum it up in one sentence for you:

 

Classes add another layer of scope to the environment.

 

The End.  If you already understand how a functions work (noticing that a chunk of code is being reused over and over so you group it together, slap a label on it and just call it by name, passing/returning variables, variable scope, etc..) then you are 99% there as far as classes are concerned. 

 

Class properties are simply variables that operate within a class.  Class methods are regular run of the mill functions that operate within a class.  There are lots of different features and syntaxes and "permissions" for property/method accessibility that go along with it; it is a very robust upgrade to what functions offer, but it all still boils down to that single statement.

 

 

 

Thanks CV!

 

yes that's pretty much what I meant but I will clarify just to make sure. I remember reading, perhaps for C++ that if you don't create a constructor, then the compiler will create one for you by default, so there is always a constructor no matter what, even if it's not defined in the class.

 

so I was just curious that, if the parent class defined a constructor with unique arguments passed to it when instantiating it, what would the subclass do if you didn't define one? Would it then inherit the parent's constructor? Therefore when you try to instantiate the subclass, you must also pass arguments to it that you would have needed to do for the parent?

 

Thanks again for your very very quick and thorough responses, I was not expecting anyone to answer my questions so quickly!! I am definitely going to be around much more now!!

 

 

Link to comment
Share on other sites

yes that's pretty much what I meant but I will clarify just to make sure. I remember reading, perhaps for C++ that if you don't create a constructor, then the compiler will create one for you by default, so there is always a constructor no matter what, even if it's not defined in the class.

Okay, to understand this, understand that php is not compiled, it is interpreted.  The difference between compiled code and interpreted code is this:  compiled code can be executed by itself, without the compiler.  I can write a program in c++, compile it, and I can execute it on a machine that does not have the c++ compiler on it.  Interpreted code needs the compiler to run.  So I cannot write a php script and run it on a machine that doesn't have php installed on it.

 

The reason why languages like c++ create a default constructor class is because the code is compiled into a stand alone program.  Therefore it needs to know everything.  It's the same reason why you can't just create variables on-the-fly in a compiled language (you have to declare them first, even their type).  It needs those things made, and those boundaries set, because it doesn't have the compiler there to hold its hand telling it what to do when something goes awry.  So in a way, compiled programs are standalone because they have a sort of mini-interpreter built into them.

 

So when you don't have a constructor class in c++, a default one is made, but it is empty.  It doesn't actually do anything.  Since php code is interpreted, there's no need to create some empty constructor class as default, because it is just parsing the code and spitting out results as it comes.

 

so I was just curious that, if the parent class defined a constructor with unique arguments passed to it when instantiating it, what would the subclass do if you didn't define one? Would it then inherit the parent's constructor? Therefore when you try to instantiate the subclass, you must also pass arguments to it that you would have needed to do for the parent?

 

If you define a constructor in a parent class, a child class will inherit it.  If the child class defines a constructor, the parent class's constructor will be overridden.  If a child class does not define a constructor, it will treat the parent's constructor as its own.  So if the parent class requires arguments to be passed, then you will have to pass arguments to the child class when you instantiate an object of that child class.  Example:

 

<?php

class parentClass {

   function __construct ($greeting) {
      echo $greeting;
   } // end __construct
} // end class parentClass

class childClass extends parentClass {
   // nothing actually in this class except
   // for what is inherited from parentClass
} // end class childClass

// instantiating object from parent class
$someObject = new parentClass; // error: missing argument
$someObject = new parentClass("hello"); // correct

/***  instantiating object from child class ****/
// even though there is no constructor in childClass, this will give you
// the same error as in the first example above. 
$someObject = new childClass; // error: missing argument
$someObject = new childClass("hello"); // correct

?>

Link to comment
Share on other sites

 

If you define a constructor in a parent class, a child class will inherit it.  If the child class defines a constructor, the parent class's constructor will be overridden.  If a child class does not define a constructor, it will treat the parent's constructor as its own.  So if the parent class requires arguments to be passed, then you will have to pass arguments to the child class when you instantiate an object of that child class.  Example:

 

<?php

class parentClass {

   function __construct ($greeting) {
      echo $greeting;
   } // end __construct
} // end class parentClass

class childClass extends parentClass {
   // nothing actually in this class except
   // for what is inherited from parentClass
} // end class childClass

// instantiating object from parent class
$someObject = new parentClass; // error: missing argument
$someObject = new parentClass("hello"); // correct

/***  instantiating object from child class ****/
// even though there is no constructor in childClass, this will give you
// the same error as in the first example above. 
$someObject = new childClass; // error: missing argument
$someObject = new childClass("hello"); // correct

?>

 

 

That's it!! You answered exactly what I was curious about! You are the man!

 

BTW, I am using Aptana for my IDE/PHP code writing. Do you have any opinions on Aptana, Eclipse or Zend Studio? I hold no loyalty to any of these programs

since I am new. For the past 8+ years, I have  been using UltraEdit for coding but I think I want something a bit more PHP minded so I can take advantage of PHP related things....

 

 

Link to comment
Share on other sites

p.s.-

 

Note though, that that is only true if the child class is not overriding the parent class's constructor.  If the child class has its own constructor, then you would follow the rules for that constructor, not the parent's.  So if you do this:

 

class parentClass {

   function __construct ($greeting) {
      echo $greeting;
   } // end __construct
} // end class parentClass

class childClass extends parentClass {
   function __construct () {
      // example
   } // end __construct

} // end class childClass

// this works.  childClass construct overrides parentClass construct
// childClass construct requires no arguments to be passed to it
$someObject = new childClass; 
// this also 'works' but does not do what you may think!
// remember, childClass construct overwrites the parent class's constructor
// so it's just like passing arguments to a regular function that doesn't
// have any arguments declared
$someObject = new childClass("hello"); 

Link to comment
Share on other sites

BTW, I am using Aptana for my IDE/PHP code writing. Do you have any opinions on Aptana, Eclipse or Zend Studio? I hold no loyalty to any of these programs

since I am new. For the past 8+ years, I have  been using UltraEdit for coding but I think I want something a bit more PHP minded so I can take advantage of PHP related things....

 

I haven't had any need for a full blown IDE.  I use html-kit.  I like syntax highlighting, being able to have multiple files open at the same time, and built-in remote file editing.  Anything else I don't need/use.  But that's just me. 

Link to comment
Share on other sites

p.s.-

 

Note though, that that is only true if the child class is not overriding the parent class's constructor.  If the child class has its own constructor, then you would follow the rules for that constructor, not the parent's.  So if you do this:

 

class parentClass {

   function __construct ($greeting) {
      echo $greeting;
   } // end __construct
} // end class parentClass

class childClass extends parentClass {
   function __construct () {
      // example
   } // end __construct

} // end class childClass

// this works.  childClass construct overrides parentClass construct
// childClass construct requires no arguments to be passed to it
$someObject = new childClass; 
// this also 'works' but does not do what you may think!
// remember, childClass construct overwrites the parent class's constructor
// so it's just like passing arguments to a regular function that doesn't
// have any arguments declared
$someObject = new childClass("hello"); 

 

 

Thanks CV,

 

In your example, the last part, you created an object with the childClass("hello"), did you mean to use ParentClass("Hello") ???

 

Since the childClass in the example required no arguments, if you passed an argument, would it be a fatal error when instantiating the childClass?

 

 

 

 

 

I haven't had any need for a full blown IDE.  I use html-kit.  I like syntax highlighting, being able to have multiple files open at the same time, and built-in remote file editing.  Anything else I don't need/use.  But that's just me. 

 

Ok thanks, just wondering.

Link to comment
Share on other sites

In your example, the last part, you created an object with the childClass("hello"), did you mean to use ParentClass("Hello") ???

 

No, I did it right.  The intention was to show what happens when you instantiate an object of a class that has a constructor, trying to use follow the rules of its parent class that has a constructor.

 

Since the childClass in the example required no arguments, if you passed an argument, would it be a fatal error when instantiating the childClass?

 

No, you do not get a fatal error.  You don't even get a warning or notice.  php simply discards any passed arguments beyond what is defined in the function (or class method).  If it requires no arguments and you pass 1+ they will simply be ignored.  If it requires 1 argument and you do 2+, same thing.  It only yells at you if you have less than what is required.

 

TBH I'm not sure why this is.  For instance, javascript will throw an error at you, saying that you are trying to pass extra arguments (it will still run though, so it's really like a warning/notice).  Not really sure why this is not at least a warning or notice.

Link to comment
Share on other sites

In your example, the last part, you created an object with the childClass("hello"), did you mean to use ParentClass("Hello") ???

 

No, I did it right.  The intention was to show what happens when you instantiate an object of a class that has a constructor, trying to use follow the rules of its parent class that has a constructor.

 

Since the childClass in the example required no arguments, if you passed an argument, would it be a fatal error when instantiating the childClass?

 

No, you do not get a fatal error.  You don't even get a warning or notice.  php simply discards any passed arguments beyond what is defined in the function (or class method).  If it requires no arguments and you pass 1+ they will simply be ignored.  If it requires 1 argument and you do 2+, same thing.  It only yells at you if you have less than what is required.

 

TBH I'm not sure why this is.  For instance, javascript will throw an error at you, saying that you are trying to pass extra arguments (it will still run though, so it's really like a warning/notice).  Not really sure why this is not at least a warning or notice.

 

 

Great got it!! I completely understand....for now!! Thanks buddy!!

 

 

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.