Page 1 of 1

Php question..what is this code?

Posted: Thu Feb 03, 2011 7:08 pm
by Ravinos
As I've said before, I knew nothing of PHP before coming to this site and following the way Halls wrote his code...lately I've stumbled across PHP that is formatted way different. He is an example?

Code: Select all

class Facebook
{
  public function getUser() {
    $session = $this->getSession();
    return $session ? $session['uid'] : null;
  }
}

Code: Select all

$facebook = new Facebook(array(
  'appId' => 'some number',
  'secret' => 'some number',
  'cookie' => true,
));

Code: Select all

$session = $facebook->getSession();

Code: Select all

if ($session) {
    $uid = $facebook->getUser();
    echo $uid ;
}
else{
echo "No session.";
}
With the above code what does -> mean? Is it some kind of function navigation command? Facebook being the class and getUser being a function in that class.To me it looks very close to the way that Actionscript 3.0 is laid out when using the OOP method of Classes and functions. I think I just answered my own question but it never hurts to ask for verification :)

Re: Php question..what is this code?

Posted: Thu Feb 03, 2011 9:10 pm
by kaos78414
See this: http://stackoverflow.com/questions/1245 ... mysqli-oop

The first answer has a great explanation. Happy coding! :D

Re: Php question..what is this code?

Posted: Thu Feb 03, 2011 9:27 pm
by Ravinos
Thank you that is exactly what I was looking for. Makes sense now. I have seen the :: method before and never understood what it meant.

The facebook connect / facebook integration method I'm using uses this setup. Even though they no longer update the PHP api for facebook connect I guess it will always be available as a secondary method. The new standard is to use the Javascript SDK and their javascript api.

Re: Php question..what is this code?

Posted: Thu Feb 03, 2011 10:21 pm
by Jackolantern
Procedural programming is a lot easier for beginners to pick up on, which is why the tutorials are written that way, but OOP PHP (the style of code you posted above) is much more organized and lends itself to large application development much better. Procedural code is much more difficult to maintain and extend in the future.

Re: Php question..what is this code?

Posted: Mon Feb 07, 2011 3:01 am
by Xaleph
All true, except that OO is not that hard to learn. In fact, OO is easier to learn because it can be applied to any programming language ( with a few exceptions, obviously ).

The whole OO is not based on PHP, it`s based on a way of thinking. If you learn OO you can learn any other programming language quickly. The whole concept behind OO stays the same, regardless of the language. So, by learning OO, you will learn PHP faster, since you get a grasp at what variables are, methods, object etc.

Just my opinion though.

Re: Php question..what is this code?

Posted: Mon Feb 07, 2011 3:55 am
by Jackolantern
While it is very true that OOP translates from language to language very well, it is a pretty well-accepted industry idea that OO is more difficult than procedural programming. And it is logical that it is, considering the fact that OO also encompasses procedural programming. Inside of methods, constructors and other executable blocks of code you are actually writing procedural code. OO simply wraps around procedural programming. It makes learning second languages and on easier, but it makes learning a first language quite a bit harder to learn to use well. OO is a much more abstract way of looking at programming, and beginners regularly have a lot of problems with it. Books and training videos typically try to insulate beginners from the details of OO for as long as possible while learning basic programming. Learning programming logic and OO at the same time has traditionally been very difficult for newcomers.

Re: Php question..what is this code?

Posted: Mon Feb 07, 2011 4:39 am
by Xaleph
True to some extend, however, learning OO doesn`t mean by default you should learn it trough a language. Grasping the concept behind OO it way more important then simple syntax errors and bugs.

I would also like to know what you mean with procedural programming inside OO. Because the whole concept of OO means no precedural programming. It is true that within methods you want to execute some code, but this is not defined as procedural programming. Each block of code within a wrapper, be it a object or static class means it`s a method, not a piece of procedural coding. In browser languages this is debateble, however, in runnable programs, be it an .exe or .dmg or whatever, every method is callable any time. This means it doesn`t execute once it reaches a certain point, it executes when it is being called.

It is all passed by-reference. As you may well know, not passed-by-variable.

Re: Php question..what is this code?

Posted: Mon Feb 07, 2011 5:03 am
by Jackolantern
What I mean is that, inside of a method, event listener, constructor, etc., the code executes in top-to-bottom fashion and calls other methods (which in procedural programming would be the same as procedures/routines/subroutines). That is like procedural programming. For example, while it would be a horrifying misuse of the language, you can actually program procedurally in Java. You would just build a single class, and do everything in the main() function, and just write tens or hundreds of methods to call from within main(), completely ignoring other classes. At that point the single class simply becomes the code file, and you are coding procedurally. Of course it would technically still have to be called OO because of the single class, but you get the idea. "Procedural programming" is such a basic concept that almost every paradigm, if not every paradigm, still has a lot in common with it. Object-oriented programming is built on top of procedural programming, not in isolation from it, just like how C++, an OO language, was built on top of C, a procedural language. That is what I mean that OOP "wraps" procedural programming.

Re: Php question..what is this code?

Posted: Mon Feb 07, 2011 5:51 am
by PaxBritannia
The thing I don't like about PHP being OOP, is that it's not OOP. (Does this make any sense? :?: )

In PHP, it's not really object-oriented programming as much as object-capable programming. I mean, you can use objects and classes, but PHP isn't really oriented towards it. The underlying structure in PHP is still procedural. A good way to explain it would be 'glorified functions'.

OOP is great for larger projects when you start collecting a code-repository, as it makes re-using code extremely easy. The potential negative here is the overhead. As with functions, every time a class is called, it produces more overhead for the servers, making it slower to run. Of course, considering the time saved developing vs. the overhead for your servers, normally OOP is worth it. :)

pax.

Re: Php question..what is this code?

Posted: Mon Feb 07, 2011 9:48 am
by Xaleph
Sure you are absolutely right regarding PHP not being a true OO language. But then again, i never said it was, right? I Still believe OO way easier to learn if you don`t learn it by language, but by design. If you understand the core concept of OO programming becomes much more logical.

As far as code base and possible overhead: geez, no comment. It`s true that OO is == overhead but yeah, like you said, it definitly outweights the other alternative, procedural programming, which is 9x10^x harder to maintain in the long run!

Anyway, my comment from yesterday ( previous ) was written a little late on my side, so i suppose it`s full of error anyway.