Php oop!

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

  • GumSlone
    replied
    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();
    }

    }
    ?>

    Leave a comment:


  • antony2kx
    replied
    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 

    Leave a comment:


  • something else
    replied
    I mean if i have got a class like:
    PHP Code:
    <? 
    class someclass{

    funtion function1(){
     //do something here
    }

    funtion function2(){
      //how do i use function1 here?
    // $someclass->function1();  //tried this and it caused errors
    // or is it $this->function1(); //i havent tried using this
    }

    }
    ?>

    Leave a comment:


  • GumSlone
    replied
    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 ?
    Not oop: function_name();

    Oop: $this->function_name();

    Leave a comment:


  • something else
    replied
    im a noob at oop ..... how do you call for a function within a function within the same class ?

    Leave a comment:


  • StunningNick
    replied
    this

    Originally posted by softwarefreak View Post
    $this represents the object inside the class
    i think "$this" represent non-static call , don't get me wrong, I'm an noob willing to learn :P
    none of us is wrong technically/programatically...

    "this" represent an non-static call to an object/method/variable bla bla bla etc... thanks for correction Dude :cheers:

    Leave a comment:


  • softwarefreak
    replied
    Originally posted by StunningNick View Post
    i think "$this" represent non-static call , don't get me wrong, I'm an noob willing to learn :P
    Ok master, got it :D

    added after couple of minutes :D (just phped php.net for perfect answer)

    $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object
    Last edited by softwarefreak; 18.03.12, 19:19.

    Leave a comment:


  • StunningNick
    replied
    $this

    Originally posted by softwarefreak View Post
    1st one is correct

    $this represents the object inside the class
    PHP Code:
    class mycls {
    public 
    $name;
    public function 
    me()
    {
    return 
    "Hi";
    }
    public function 
    hehe()
    {
    //inside the class
    echo $this->me(); 
    }
    }
    $object = new mycls;
    echo 
    $object->me();
    //look at the above code, you had to create an instance of the class but inside the class you can use $this :P :D 


    i think "$this" represent non-static call , don't get me wrong, I'm an noob willing to learn :P

    Leave a comment:


  • CreativityKills
    replied
    OOP can be a force for both good and evil. Evil if you don't know what you're doing.

    Leave a comment:


  • shushant
    replied
    Originally posted by softwarefreak View Post
    I know dat, you know but I tried to show the use of $this
    oreilly OOP e-book for a clear understanding, it's very helpful
    Don't take me as wrng i was in need of help In OOP :p Having Problem understaning The use of $this and Some other Variables

    Leave a comment:


  • softwarefreak
    replied
    Originally posted by shushant View Post
    :P i know had to create an instance make it wrk
    I know dat, you know but I tried to show the use of $this
    oreilly OOP e-book for a clear understanding, it's very helpful

    Leave a comment:


  • shushant
    replied
    Originally posted by softwarefreak View Post
    1st one is correct

    $this represents the object inside the class
    PHP Code:
    class mycls {
    public 
    $name;
    public function 
    me()
    {
    return 
    "Hi";
    }
    public function 
    hehe()
    {
    //inside the class
    echo $this->me(); 
    }
    }
    $object = new mycls;
    echo 
    $object->me();
    //look at the above code, you had to create an instance of the class but inside the class you can use $this :P :D 
    :P i know had to create an instance make it wrk

    Leave a comment:


  • softwarefreak
    replied
    1st one is correct

    $this represents the object inside the class
    PHP Code:
    class mycls {
    public 
    $name;
    public function 
    me()
    {
    return 
    "Hi";
    }
    public function 
    hehe()
    {
    //inside the class
    echo $this->me(); 
    }
    }
    $object = new mycls;
    echo 
    $object->me();
    //look at the above code, you had to create an instance of the class but inside the class you can use $this :P :D 

    Leave a comment:


  • shushant
    replied
    Any1 of u Can Tell me
    that is dis right way to access functions in class
    Code:
    class mycls {
    public function me(){
    return "Hi";
    }
    public function hehe(){
    echo $this->me();
    }
    }
    or this
    Code:
    class mycls {
    public function me(){
    return "Hi";
    }
    public function hehe(){
    echo me();
    }
    }
    nd what is use of $this

    Leave a comment:


  • morse
    replied
    For large applications, PHP OOP is good but for small applications PHP OOP is bad

    Leave a comment:

Working...
X