Jump to content

New to Php from .net - few OOP questions


derstuka

Recommended Posts

Hello.  I just started learning php.  All of my experience thus far has been in .Net, so please bare with me as most of my questions will be in the format of "in c# you did it like this ... what is the php equivelant?"

 

Function overloading:

In c#, I would do (in same class):

 

public void Bark(){Woof;}

 

public void Bark(int howManyTimes){Woof * howManyTimes;}

 

---

Pass arguments to base class' constructor

class Pitbull : Dog

{

  public Pitbull(string name, int age) : base(name, age) {}

}

 

 

Thanks in advance.

Link to comment
Share on other sites

As for overloading, PHP doesn't support traditional method overloading like in C# or Java as far as I know. What is called as "Overloading" in PHP5 are ways of creating objects methods and members on the fly, rather than different functions named the same.

 

I believe you can emulate overloading somewhat in a number of ways. The first thing that comes to mind without any further searching is the func_num_args() and func_get_arg() functions, which returns the number of arguments passed to it, and the said arguments . So you might do:

 

<?php

public function bark()
{
   $numArgs = func_num_args;
    if ($numArgs == 1) {
        //bark()'s a number of times specified by a single argument if it's provided.
        for ($i=0; $i<func_get_arg(0); $i++) {
            $this->woof();
        }
    } else {
        //If no arguments, or more than one are provided, bark()'s a single time.
        $this->woof();
    }
}

//Somwhere else in the code you can call:
$object->bark(); //func_num_args() will return 0
$object->bark(3); //func_num_args() willl return 1, func_get_arg(0) will return 3.
?>

 

Another solution would be to specify all arguments with default values, for example:

 

<?php

public function bark($numTimes = 1)
{
    if ($numTimes > 1) {
        //bark()'s a number of times specified by $numTimes.
        for ($i=0; $i<$numTimes; $i++) {
            $this->woof();
        }
    } else {
        //bark()'s a single time.
        $this->woof();
    }
}

//Somwhere else in the code...

$object->bark(); //$numTimes defaults to 1
$object->bark(3); //$numTimes is now 3
?>

 

As for the second question, passing arguments to the base class constructor (If I get your question correctly).

Be aware that the child class automatically inherits from the base class any properties or methods defined in it, including it's constructor. Suppose this two classes:

 

<?php

class Dog
{
    protected $name, $age;

    public function __construct($name, $age)
    {
        $this->name = $name:
        $this->age = $age;
    }
}
?>

 

And a child: (Notice it has no defined constructor nor properties)

<?php

class Pitbull extends Dog
{
    public function barkAge()
    {
        echo $this->name . 'barks that he is' . $this->age . 'years old';
    }
}
?>

 

This is completely valid code:

<?php

$dog = new Pitbull('Spike', 4);
$dog->barkAge();

//Will output: "Spike barks that he is 4 years old"

?>

 

The pitbull object has access to all it's parent's methods (including the constructor) and its methods. You can, of course, override them. Like:

 

<?php

class Pitbull extends Dog
{
    protected $furColor;

    public function __construct($furColor)
    {
        $this->furColor = $furColor;
    }
}
?>

 

A pitbull object still has its $name and $age properties, but now the constructor overrides his parent's and sets a $furColor instead. If you wanted to keep the parent's functionality, you can do:

 

<?php

class Pitbull extends Dog
{
    protected $furColor;

    public function __construct($name, $age, $furColor)
    {
        parent::__construct($name, $age);
        $this->furColor = $furColor;
    }
}
?>

 

And voila!

Hope that helps clear up things on PHP's OOP. Cheers.

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.