Php oop!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    example OOP wapcreate.com is pure OOP and is built upon MVC architecture

    PHP Code:

    class Car {

    private 
    $carname;
    private 
    $caryear;
    public 
    object;

    public function 
    __construct($carname$caryear)
    {
       
    self::setName($carname); //this could also be done using $this->setName($carname) or Car::setName($carname)
       
    self::setYear($caryear);
    }

    public function 
    setYear($y)
    {
       
    $this->caryear $y;
    }

    public function 
    setName($n)
    {
       
    $this->carname $n;
    }


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

    public function 
    getYear()
    {
      return 
    $this->caryear;
    }

    }; 
    // the semicolon is not needed here

    usage of this class will be 

    $car 
    = new Car("Lamboginy""2010");

    you can now use the get methods to access properties of this class

    like if you wanted to print the year of the car it will be like

    echo $car->getYear(); will print 2010;

    you can also have objects within other objects like 

    $car
    ->object = new Car("BMW""2011");

    echo 
    $car->object->getName(); will print BMW 

    Comment


      #17
      PHP Code:
      <? 
      class someclass{

      function function1(){
       //do something here
      }

      function function2(){

      $this->function1();
      }

      }
      ?>
      or outside
      PHP Code:
      $c = new someclass;
      $c->function1();
      $c->function2(); 
      call function3 in a class which is not inside the class:
      PHP Code:
      <? 
      function function3()
      {
      return 'do something';
      }

      class someclass{

      function function1(){
       //do something here
      }

      function function2(){
      $something_inside = $this->function1();
      $something_outside = function3();
      }

      }
      ?>
      Advertise your mobile site for FREE with AdTwirl

      Comment


        #18
        im guessing if you have got 2 classes then to use a function from the second class in the first class function you would use the same method as calling it outside the class?
        eg:
        PHP Code:
        <?
        class someclass{

        function function1(){
        $c = new someclass2; 
        $c->function2();
        }

        }

        class someclass2{

        funtion function2(){

        }

        }
        ?>

        Comment


          #19
          That is one way at something else

          or you can do it by extending the first class by the second one

          for example


          PHP Code:


          class Class1{

          public function 
          functionInClassOne()
          {
          echo 
          "hello worlds";
          }



          }


          class 
          Class2 extends Class1{

          public function 
          printHelloWorld()
          {
             
          $this->functionInClassOne(); //this will use the methods in class 1 because u have extended it
          }

          }

          so instance would be 

          $instance 
          = new Class2;

          $instance->printHelloWorld(); 

          as easy as it gets

          Comment


            #20
            what im a little confused about when using oop it makes your script a lot longer
            does this make it longer to process?

            Comment


              #21
              Nope it does not is is very useful when creating large scale websites or classifying and modeling data Correctly, especially if you use AN OBJECT RELATIONAL DATABASE its good to use classed to model all your table data etc, a class is like a structure that can store information of the same data-type or classification.

              So it makes your code cleaner and easier to read

              Comment


                #22
                is this possible in php procedural programming?

                I want a variable that can be accessed inside all the functions,

                like
                PHP Code:

                $api 
                "4y3g3h3h3";

                function 
                some_func()
                {
                echo 
                $api;

                I didn't found one so used the oo way..

                PHP Code:
                class some_class
                {
                public 
                $api "4y3g3h3h3";

                public function 
                some_func()
                {
                echo 
                $this->api
                }

                I need some facebook likes, can you please help me
                http://facebook.com/softwarefreakin
                I noticed social media is really powerful
                Well DONE is better than well SAID

                Comment


                  #23
                  Originally posted by something else View Post
                  im a noob at oop ..... how do you call for a function within a function within the same class ?
                  :D oop is just a way to write codes differently, A person writing OOP style code may not hold the knowledge like you coding in procedural manner :P

                  if you don't feel like "ohhh, the codes are so messy in my script", then carry on w/ your own way, & if yes, try OOP, I mean learn it when free ;)
                  Last edited by softwarefreak; 19.03.12, 10:43. Reason: 2much LOL'ing
                  I need some facebook likes, can you please help me
                  http://facebook.com/softwarefreakin
                  I noticed social media is really powerful
                  Well DONE is better than well SAID

                  Comment


                    #24
                    i still dont get the real advantages of using oop .... i know in another topic gum mentioned being able to have similar function names but that doesnt seem to be worth it as without using oop you could have to functions one with 1 letter different in the name....
                    and the other reason i have forgot .....

                    i would also like to know what the dangers are of using oop:

                    Originally posted by CreativityKills View Post
                    OOP can be a force for both good and evil. Evil if you don't know what you're doing.
                    i dont want to start coding something that can be hacked easily

                    Comment


                      #25
                      hmm...

                      Originally posted by something else View Post
                      im a noob at oop ..... how do you call for a function within a function within the same class ?


                      i still dont get the real advantages of using oop ....
                      depends what type are you calling , possibilities are ...{PHP}
                      $this->method();
                      self::method();
                      parent::method();
                      static::method();
                      ClassName::method();


                      readability
                      flexibility
                      reusability (it's hard to explain, will try .. )
                      but evil for small application

                      suppose , social network application is there with following functionality ,
                      picture_gallery.php
                      user_list.php
                      message.php



                      obviously pagination is required ,
                      then why not create an class with all commonly required method() rather than implementing it for every other

                      both are in own ways :D bla bla bla lecture over :P
                      Last edited by StunningNick; 19.03.12, 21:50. Reason: not*(no) :P

                      Comment


                        #26
                        main advantages
                        -> Easy for dividing problems into different categorys.
                        -> oop protects data much better than procedural way.
                        -> Whole Data resides within classes so u can easily edit/extend it in future.
                        ->Reusability (a solution of single big problem can be used while solving bigger problems)
                        and NO. IT DOESNT HAS ANY SECURITY ISSUES..

                        Comment


                          #27
                          Originally posted by softwarefreak View Post
                          is this possible in php procedural programming?

                          I want a variable that can be accessed inside all the functions,

                          like
                          PHP Code:

                          $api 
                          "4y3g3h3h3";

                          function 
                          some_func()
                          {
                          echo 
                          $api;

                          I didn't found one so used the oo way..

                          PHP Code:
                          class some_class
                          {
                          public 
                          $api "4y3g3h3h3";

                          public function 
                          some_func()
                          {
                          echo 
                          $this->api
                          }

                          PHP Code:
                          $api "4y3g3h3h3"

                          function 
                          some_func() 

                          global 
                          $api;
                          echo 
                          $api

                          echo 
                          some_func(); 
                          Advertise your mobile site for FREE with AdTwirl

                          Comment


                            #28
                            Originally posted by GumSlone View Post
                            PHP Code:
                            $api "4y3g3h3h3"

                            function 
                            some_func() 

                            global 
                            $api;
                            echo 
                            $api

                            echo 
                            some_func(); 
                            I know the use of declaring a var globally but,
                            But I meant to say the same var in multiple functions, is it possible?

                            function some_func()
                            {
                            // using $api here
                            }
                            function some_othr_func()
                            {
                            // using $api here also
                            }
                            I need some facebook likes, can you please help me
                            http://facebook.com/softwarefreakin
                            I noticed social media is really powerful
                            Well DONE is better than well SAID

                            Comment


                              #29
                              Originally posted by softwarefreak View Post
                              I know the use of declaring a var globally but,
                              But I meant to say the same var in multiple functions, is it possible?

                              function some_func()
                              {
                              // using $api here
                              }
                              function some_othr_func()
                              {
                              // using $api here also
                              }
                              yes i know that you know it, but you asked so i replied
                              this will be done with global,
                              or by defining a global variable like PHP: $GLOBALS - Manual or PHP: define - Manual
                              in oop you have to use global too if you want to access for exaple database class
                              Advertise your mobile site for FREE with AdTwirl

                              Comment


                                #30
                                I Like sql_autoload_register as it is time saver
                                coz does'nt need to require class in core or config file.
                                Nd then starting up new instization of class
                                Code:
                                $clas = new class;
                                to avoid it
                                just simple put
                                Code:
                                sql_autoload_register(function($class){
                                require $class.'.php';
                                });
                                then
                                use
                                Code:
                                $clas = new clas;
                                as it will auto require class on instization :D

                                Comment

                                Working...
                                X